regex - I can't accurately understand how does JavaScript's method string.match(regexp)'s g flag work -
in book "javascript: parts", explains method string.match(regexp)
below:
the match method matches string , regular expression. how depends on g flag. if there no g flag, result of calling string .match( regexp ) same calling regexp .exec( string ). however, if regexp has g flag, produces array of matches excludes capturing groups:
then book provides code example:
var text = '<html><body bgcolor=linen><p>this <b>bold<\/b>!<\/p><\/body><\/html>'; var tags = /[^<>]+|<(\/?)([a-za-z]+)([^<>]*)>/g; var a, i; = text.match(tags); (i = 0; < a.length; += 1) { document.writeln(('// [' + + '] ' + a[i]).entityify()); } // result // [0] <html> // [1] <body bgcolor=linen> // [2] <p> // [3] // [4] <b> // [5] bold // [6] </b> // [7] ! // [8] </p> // [9] </body> // [10] </html>
my question can't understand "but excludes capturing groups".
in code example above, html
in </html>
in capturing group. , why still included in result array?
and /
in </html>
in capturing group. , why included in result array?
could explain "but excludes capturing groups" code example above?
thank much!
in code example above, html in in capturing group. , why still included in result array?
because it's full match. when says "but excludes capture groups" doesn't mean full match result, contents of capture groups aren't reiterated in array. if capturing groups included, you'd see
// result // [0] <html> // [1] // capture group; nothing here // [2] html // capture group // [3] // capture group; nothing here // ...
and / in in capturing group. , why included in result array?
for same reason above: it's part of overall match, , that's what's in result; contents of individual capture groups not.
this easier understand simpler example. consider code:
var s = "test1 test2"; var re = /(test)(.)/g; var r = s.match(re); var i; (i = 0; < r.length; ++i) { console.log("[" + + "]: '" + r[i] + "'"); }
because regular expression has g
flag, full matches included in array, see:
[0]: 'test1' [1]: 'test2'
in each case, entry in array full match, includes characters matched within capture groups making overall expression.
if removed g
flag didn't change else, we'd first full match followed contents of 2 capture groups:
[0]: 'test1' // full match, including stuff each capture group [1]: 'test' // capture group 0's contents [2]: '1' // capture group 1's contents
there, first entry full match; second , third contents of capture groups. note contents of capture gruops
Comments
Post a Comment