Raul Ribeiro
Raul Ribeiro

Reputation: 1

pretty complex Regex

I did a lot of research, but apparently my Regex skills are not enough to solve this, so I come to humbly ask for advice.

I have a JS var that gets strings similar to this:

"value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i"

it doesn't have a fixed lenght. it may return 1 or 20 values, there's no way to know.

As you can see, it returns a big sequence of values that are always between "value=" and "&", except for the last one, that has no "&" in the end. I need to parse that, and get this in the end:

8kadctgwqqe0
8kaczvfgoyrs
8kwkgz2ysm1i

I don't even know where to start... Thanks a lot!

Upvotes: 0

Views: 61

Answers (4)

exussum
exussum

Reputation: 18550

personally I would avoid regex for this.

if your string is always in the value=SOMETHING&value=SOMETHINGELSE format use this

string.split("value=").join("").split("&")

according to How to replace all occurrences of a string in JavaScript? its actually quicker to use this over regex also

var string = "value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i"
var yourArray = string.split("value=").join("").split("&")
["8kadctgwqqe0", "8kaczvfgoyrs", "8kwkgz2ysm1i"]

You may need to add some tests to ensure the correct format of string if you are not sure about it.

Upvotes: 3

James Wilkins
James Wilkins

Reputation: 7367

This works as well:

str.match(/value=([^&]*)/g).join("").split('value=').splice(1)

Upvotes: 1

Downgoat
Downgoat

Reputation: 14361

The following RegExp/.filter() should do it:

'value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i'.match(/([^=&]+)/g).filter(function(a){return a!='value'});

The .match() will grab the separate query values (and the key names). In order to deal with this, we use .filter() to remove the value results. This will leave it with the correct results.


Sometimes it's better to use regular JavaScript string manipulation (even though this looks like the worst answer)

.filter() is awesome, more info on it here

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476699

You can use the following regex:

/(?:^|&)value=([^&]*)/g

regex101 demo

The matches are then stored in the first capture group.

You can now access the capture groups using this method:

var myRegexp = /(?:^|&)value=([^&]*)/g;
var myString = "value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i";

match = myRegexp.exec(myString);
while (match != null) {
    console.log(match[1]);
    match = myRegexp.exec(myString);
}

If you are interested in the keys as well, you can use:

/(?:^|&)([^=&]*)=([^&]*)/g

In that case for each iteration, the key can be found in the first match, and the value in the second one:

var myRegexp = /(?:^|&)([^&=]*)=([^&]*)/g;
var myString = "value=8kadctgwqqe0&value=8kaczvfgoyrs&value=8kwkgz2ysm1i";

match = myRegexp.exec(myString);
while (match != null) {
    console.log(match[1]); //key
    console.log(match[2]); //value
    match = myRegexp.exec(myString);
}

Upvotes: 1

Related Questions