Home > Web Front-end > JS Tutorial > Characteristics of global mode in JavaScript regular expressions_javascript skills

Characteristics of global mode in JavaScript regular expressions_javascript skills

PHP中文网
Release: 2016-05-16 15:13:32
Original
1876 people have browsed it

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);
  }
Copy after login

Finally Result:

//0:baaabb
//0:aaabb
//0:aaab
//0:aaa
Copy after login

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);
  }
Copy after login

The final result is:

//0:baaab
//0:aaa
Copy after login

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)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template