Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda