Alex
Alex

Reputation: 65952

Need help understanding Javascript's .match method

I understand that .match() returns an array of the matches, or null if none are found. But how do I go about accessing the values of capturing groups used with .match?

For example:

var val = whatever.match('(?:^|;) ?' + stuff + '=([^;]*)(?:;|$)');

Assuming the regular expression matches more than once, how do I access the value of the capturing group in a particular match?

Thanks!!

Upvotes: 3

Views: 3250

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163268

Use array notation: [0], [1], etc.

var val = whatever.match('(?:^|;) ?' + stuff + '=([^;]*)(?:;|$)');
if(val != null) {
    var first = val[0];
    //...
}

Upvotes: 4

Related Questions