开发者

Need help understanding Javascript's .match method

开发者 https://www.devze.com 2023-01-07 12:17 出处:网络
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?

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开发者_JAVA技巧 more than once, how do I access the value of the capturing group in a particular match?

Thanks!!


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

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

精彩评论

暂无评论...
验证码 换一张
取 消