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

122
Views
JS Split String By Multiple Paired Delimiters

I have this string that I split whenever it finds two colons.

"for exam:ple thi:s string turns into" ["for exam","pls thi", "s string turns into"]

I do this with:

text.split(/(:.*?:)/g)

Afterward I interpret a string as "inside" the delimiter if it contains the characters I expect and none of those I don't.

How do I make it also extract another delimiter like $$...$$ as well as the original :...: but also include the delimiter somewhere so I can use different logic for it?

For context I'm parsing text into different react components.:...: includes addresses to embed a certain component, while $$...$$ includes MathJax.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

UPDATED

You can try this regex with particular checks for :...: and $$...$$ patterns

const text = "for exam:ple thi:s string $$turns$$ into"

const partialStrings = text.split(/(:.*?:|\$\$.*?\$\$)/g)

for (const partialString of partialStrings) {
  if (partialString.startsWith(":")) {
    //TODO: Do your logic with `:...:` pattern
    console.log(partialString + " contains ':...:'")
    continue
  }
  if (partialString.startsWith("$$")) {
    //TODO: Do your logic with `$$...$$` pattern
    console.log(partialString + " contains '$$...$$'")
    continue
  }
  
  //no patterns
  console.log(partialString + " has no patterns")
}


OLD ANSWER

You can simply split your string with :

const data = "for exam:ple thi:s string turns into"

console.log(data.split(":"))

If you want to have similar behavior for $$ and a reusable function, you can declare a function called splitBy

const splitBy = (data, delimiter) => {
  return data.split(delimiter)
}

const data = "for exam$$ple thi$$s string turns into"

console.log(splitBy(data, "$$"))

about 4 years ago · Juan Pablo Isaza 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!