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

123
Views
RegEx Split String and Leave Delimiter

I am trying to split a string and leave the delimiter intact. I am getting pretty close but I can't figure it out fully. I have found a lot of examples on Stack Overflow but I am still running in to an issue that I can't figure out. Here is the code:

"This is a string with ${G:VarSome:G} text".split(/\$\{(.+?(}\(.+?\)|}))/g)

The above code yields:

['This is a string with ', 'G:VarSome:G}', '}', ' text']

The result I am looking for is:

['This is a string with ', '${G:VarSome:G}', ' text']

The ${G:SomeVar:G} pattern is a templating system where variables will be injected. There are other formats of variables too for example ${G:AnotherVar:1}, ${G:DifferentVar:5} etc. After I get the split string, I will do a variable lookup in the system to inject the corresponding variable.

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

0

You are having an extra capture group inside the regex, this will give you a single group.

const result = "This is a string with ${G:VarSome:G} text".split(/(\${.+?})/g);

console.log(result);

The regex that graps a single capture group looks like this. it returns all the captured inside ()

/(\${.+?})/g

This should capture the examples provided. regex101

It's build with first getting ${ and then getting one or more characters lazily (.+?) until it encounters a }.

about 4 years ago · Juan Pablo Isaza Report

0

You can use this regex for splitting:

/(\${[^}]*})/

Code:

var s = 'This is a string with ${G:VarSome:G} text';
var arr = s.split(/(\${[^}]*})/);

console.log(arr);

Here:

  • \${[^}]*}: Will match ${ followed by 0 or more non-} characters followed by }
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!