Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match the string with a regexp to get substrings between '='?

The sample string is =aa=bb=cc=dd=.

I tried

string.match(/=(\w*)=/)

but that returns only aa.

How do I find aa, bb, cc and dd from the sample string?

like image 655
batman Avatar asked Dec 27 '22 00:12

batman


2 Answers

This regex will match explicitly your requirements, and put the non, delimiter portion it the first capture group:

=([^=]+)(?==)

Unfortunately JavaScript regex does not have look behinds, otherwise this could be done in much easier fashion.

Here is some code:

var str = '=([^=]+)(?==)';

var re = /=([^=]+)(?==)/g,
    ary = [],
    match;

while (match = re.exec(str)) {
    ary.push(match[1]);
}

console.log(ary);
like image 199
Daniel Gimenez Avatar answered Jan 12 '23 15:01

Daniel Gimenez


var values = yourString.split('='); You'll get an array with all your required values.

like image 34
Gintas K Avatar answered Jan 12 '23 15:01

Gintas K