Home > Article > Web Front-end > Characteristics of global mode in JavaScript regular expressions_javascript skills
Returns a Boolean value indicating the status of the global flag (g) used by the regular expression. The default value is false. Read only. rgExp.global Required The rgExp parameter is a regular expression object. The global property returns true if the regular expression sets the global flag, otherwise it returns false. Use the global flag to indicate that the search operation will find all matching items in the string being found, not just the first one. This is also called global matching.
I have never been very clear about the performance of global JavaScript, so I did a few tests today:
var str = 'bbaaabb', reg = /^b|b$/; while(reg.test(str)){ str = str.replace(reg,''); console.log(reg.lastIndex + ":" + str); }
Finally Result:
//0:baaabb //0:aaabb //0:aaab //0:aaa
But if you make some slight modifications
var str = 'bbaaabb', reg = /^b|b$/g; while(reg.test(str)){ str = str.replace(reg,''); console.log(reg.lastIndex + ":" + str); }
The final result is:
//0:baaab //0:aaa
This result shows that in global mode, after matching the starting b character, It will also continue to match the trailing b character, thus ignoring the middle "|" operator.
This is all about the characteristics of global mode in JavaScript regular expressions. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!