if(msg.content.includes("[mid]")) {
let str = msg.content
let pokeID = str.substring(
str.indexOf("[mid]") + 5,
str.lastIndexOf("[/mid") //get the unique-code for a pokemon
);
msg.channel.send({ content: `Here is your Pokemon:`, embeds: [midData(pokeID)] });
this code works fine, I'm would to be able to put in any user text that is before or after the [mid]code[/mid]
example user inputs "this text can be of any length or even null [mid]unique-code[/mid] this text can also be of any text or null"
the output should be :
this text can be of any length or even null this text can also be of any text or null
[mid]unique-code[/mid] (which is a link)
I have tried this: https://imgur.com/a/uq8CVpn //image of output
I need 3 strings from the user input.
string1 = all text, if any before [mid]unique-code[/mid] // pokemon code
string2 = [mid]unique-code[/mid]
string3 = all text if any behind [mid]unique-code[/mid] //pokemon code
using node v16 and discord v13
thank you
You just need to use lastIndexOf and substring like this (I also assumed you meant after and not behind in string3) :
also for clarification substring works like this:
substring(startIndex, endIndex /*defaults to the end of the string if not specified*/ )
const message = "this text can be of any length or even null [mid]unique-code[/mid] this text can also be of any text or null"
const beforeCode = message.substring(0, message.lastIndexOf("[mid]")).trim()
const afterCode = message.substring(message.lastIndexOf("[/mid]") + 6).trim()
const code = message.substring(
message.lastIndexOf("[mid]") + 5,
message.lastIndexOf("[/mid]")
).trim();
console.log("beforeCode:", beforeCode, "\nafterCode:", afterCode, "\ncode:", code)
to get the text before the unique code we substring the message from index 0 (the message's start) -> start index of "[mid]"
to get the text after the unique code we substring the message from end index of "[mid/]" -> end of the string
to get the unique code we substring the message from end index of "[mid]" -> start index of [mid/]
I also use trim to remove any spaces from the start or end of the string