+&...&...&..." I would like to...">

Split using RegEx in JavaScript

Say I have a generic line

"...&<constant_word>+<random_words_with_random_length>&...&...&..." 

I would like to split the string using

 "<constant_word>+<random_words_with_random_length>&" 

for which I tried to split RegEx, for example

 <string>.split(/<constant_word>.*&/) 

This RegEx breaks up to the last '&' unfortunately, i.e.

 "<constant_word>+<random_words_with_random_length>&...&...&" 

What would RegEx code be if I wanted it to split when it got the first "&"?

example for line splitting e.g.

 "example&ABC56748393&this&is&a&sample&string".split(/ABC.*&/) 

gives me

 ["example&","string"] 

while I want ...

 ["example&","this&is&a&sample&string"] 
+4
source share
2 answers

Can you change greed with a question mark ? :

 "example&ABC56748393&this&is&a&sample&string".split(/&ABC.*?&/); // ["example", "this&is&a&sample&string"] 
+4
source

Just use the unwanted match by posting ? after * or + :

 <string>.split(/<constant_word>.*?&/) 
+2
source

All Articles