Need help understanding Javascript.match method

I understand that it .match()returns an array of matches or null if none are found. But how do I access the values ​​of capture groups used with .match?

For instance:

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

Assuming the regex matches a few times, how do I access the value of a capture group in a specific match?

Thank!!

+3
source share
1 answer

Use record massif [0], [1]etc.

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

Source: https://habr.com/ru/post/1754279/


All Articles