Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string in between 2 delimiters and include them

Giving the following string...

"Here is my very _special string_ with {different} types of _delimiters_ that might even {repeat a few times}."

... how to split it into an array using the 2 delimiters ("_", "{ and }") but also keeping the delimiters in each element of the array?

The goal is:

[
  "Here is my very ", 
  "_special string_", 
  " with ", 
  "{different}", 
  " types of ", 
  "_delimiters_", 
  "that might even ", 
  "{repeat a few times}", 
  "."
]

My best bet was:

let myText = "Here is my very _special string_ with {different} types of _delimiters_ that might even {repeat a few times}."

console.log(myText.split(/(?=_|{|})/g))

as you see, it fails to reproduce the desirable array.

like image 520
danieljaguiar Avatar asked May 23 '20 18:05

danieljaguiar


People also ask

How do you split a string by two delimiters?

To split a string with multiple delimiters in Python, use the re. split() method. The re. split() function splits the string by each occurrence of the pattern.

Can I split a string by two delimiters Python?

Python has a built-in method you can apply to string, called . split() , which allows you to split a string by a certain delimiter.


2 Answers

You may use

s.split(/(_[^_]*_|{[^{}]*})/).filter(Boolean)

See the regex demo. The whole pattern is enclosed in a capturing group, so all matching substrings are included in the resulting array after String#split.

Regex details

  • (_[^_]*_|{[^{}]*}) - Capturing group 1:
    • _[^_]*_ - _, 0 or more chars other than _ and then a _
    • | - or
    • {[^{}]*} - a {, then any 0 or more chars other than { and } and then a }

See JS demo:

var s = "Here is my very _special string_ with {different} types of _delimiters_ that might even {repeat a few times}.";
console.log(s.split(/(_[^_]*_|{[^{}]*})/).filter(Boolean));
like image 177
Wiktor Stribiżew Avatar answered Sep 23 '22 14:09

Wiktor Stribiżew


You can use the following regex which returns some undefined values, finally, you can filter the undefined values.

let myText = "Here is my very _special string_ with {different} types of _delimiters_ that might even {repeat a few times}."

console.log(myText.split(/(_.+?_)|({.+?})/g).filter(Boolean))
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 26
Ele Avatar answered Sep 25 '22 14:09

Ele