Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
dicordjs string manipulation
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

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda