How to split a string between two delimiters in JavaScript?

I know how to split usage, multiple delimiters, but I have no idea how to split a string into an array between two characters. So:

var myArray = "(text1)(text2)(text3)".split(???) //=> myArray[0] = "text1", myArray[1] = "text2", myArray[2] = "text3" 

What should I type in "???"? Or is there another approach I should use?

Creating a ") (" separator will not work, because I want to split the array into many separators, such as ">", which makes it extremely impractical to list all possible combinations of separators

+8
source share
2 answers
 .split(/[()]+/).filter(function(e) { return e; }); 

See this demo .

+7
source

Using separation between specific characters without losing any characters is not possible using JavaScript, because for this you need lookbehind (which is not supported). But since you seem to want the texts inside parentheses, instead of splitting, you could just match the longest possible string without parentheses:

 myArray = "(text1)(text2)(text3)".match(/[^()]+/g) 
+1
source

All Articles