当前位置:网站首页>JS use regular expressions in g model and non g difference

JS use regular expressions in g model and non g difference

2022-08-10 22:41:00 Breeze and drizzle_Linmumu

Meaning

g is the abbreviation of global!It is to match all matching results.

  • Without g, in the regular process, the string is matched from left to right, if the match is successful, it will not continue to match to the right;
  • With g, it will pick out the correct matching string from beginning to end

Instance

var str = 'aaaaaaaa'var reg1 = /a/var reg2 = /a/gstr.match(reg1) // The result is: ["a", index: 0, input: "aaaaaaaa"]str.match(reg2) // The result is: ["a", "a", "a", "a", "a", "a", "a", "a"]
原网站

版权声明
本文为[Breeze and drizzle_Linmumu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102158010325.html