Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split string into parts using Regex

I have a string containing placeholders which I want replace with other strings, but I would also like to split the string whenever I encounter a placeholder.

So, by splitting I mean that

"This {0} is an example {1} with a placeholder"

should become:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

parts[0] -> "This"
parts[1] -> "{0}"
parts[2] -> "is an example"
parts[3] -> "{1}"
parts[4] -> "with a placeholder"

and then the next step would be to replace the placeholders (this part is simple):

parts[0] -> "This"
parts[1] -> value[0]
parts[2] -> "is an example"
parts[3] -> value[1]
parts[4] -> "with a placeholder"

I know how to match and replace the placeholders (e.g. ({\d+})), but no clue how to tell regex to "match non placeholders" and "match placeholders" at the same time.

My idea was something like: (?!{\d+})+ | ({\d+}) but it’s not working. I am doing this in JavaScript if Regex flavor is important.

If I can also replace the placeholders with a value in one step it would be neat, but I can also do this after I split.

>Solution :

You might write the pattern as:

{\d+}|\S.*?(?=\s*(?:{\d+}|$))

The pattern matches:

  • {\d+} Match { 1+ digits and }
  • | Or
  • \S.*? Match a non whitespace char followed by any character as few as possible
  • (?= Positive lookahead
    • \s* Match optional whitespace chars
    • (?:{\d+}|$) Match either { 1+ digits and } or assert the end of the string
  • ) Close the lookahead

Regex demo

To get an array with those values:

const regex = /{\d+}|\S.*?(?=\s*(?:{\d+}|$))/gm;
const str = `This {0} is an example {1} with a placeholder`;
console.log(str.match(regex))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading