Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!