In the book "JavaScript: The Good Parts", it explains method string.match(regexp)
as below:
The match method matches a string and a regular expression. How it does this depends on the g flag. If there is no g flag, then the result of calling string .match( regexp ) is the same as calling regexp .exec( string ). However, if the regexp has the g flag, then it produces an array of all the matches but excludes the capturing groups:
Then the book provides code example:
var text = '<html><body bgcolor=linen><p>This is <b>bold<\/b>!<\/p><\/body><\/html>';
var tags = /[^<>]+|<(\/?)([A-Za-z]+)([^<>]*)>/g;
var a, i;
a = text.match(tags);
for (i = 0; i < a.length; i += 1) {
document.writeln(('// [' + i + '] ' + a[i]).entityify());
}
// The result is
// [0] <html>
// [1] <body bgcolor=linen>
// [2] <p>
// [3] This is
// [4] <b>
// [5] bold
// [6] </b>
// [7] !
// [8] </p>
// [9] </body>
// [10] </html>
My question is that I can't understand "but excludes the capturing groups".
In the code example above, html
in the </html>
is in a capturing group. And why is it still included in the result array?
And /
in the </html>
is also in a capturing group. And why is it included in the result array?
Could you explain "but excludes the capturing groups" with the code example above?
Thank you very much!