The example in this article describes the usage of JS regular expression modifier global(/g). Share it with everyone for your reference, the details are as follows: The
/g modifier represents global matching, searching for all matches instead of stopping after the first match is found. Let’s look at a classic piece of code first:
var str = "123#abc"; var noglobal = /abc/i;//非全局匹配模式 console.log(re.test(str)); //输出ture console.log(re.test(str)); //输出ture console.log(re.test(str)); //输出ture console.log(re.test(str)); //输出ture var re = /abc/ig;//全局匹配 console.log(re.test(str)); //输出ture console.log(re.test(str)); //输出false console.log(re.test(str)); //输出ture console.log(re.test(str)); //输出false
You can see that when using /g mode, the output results of executing RegExp.test() multiple times will be difference.
When creating a regular expression object, if the "g" identifier is used or its ?global attribute value is set to true, the newly created regular expression object will use the pattern to match Strings are matched globally. In global matching mode, multiple matches can be performed on the specified string to be found. Each match uses the value of the lastIndex attribute of the current regular object as the starting position to start searching in the target string. The initial value of the lastIndex attribute is 0. After a matching item is found, the value of lastIndex is reset to the position index of the next character of the matching content in the string, which is used to identify the position where to start searching the next time a match is performed. If no matching item is found the value of lastIndex will be set to 0. When the global matching flag of the regular object is not set, the value of the lastIndex attribute is always 0, and each time a match is performed, only the first matching item in the string is found.
You can verify the performance of lastIndex in /g mode through the following code:
var str = "012345678901234567890123456789"; var re = /123/g; console.log(re.lastIndex); //输出0,正则表达式刚开始创建 console.log(re.test(str)); //输出ture console.log(re.lastIndex); //输出4 console.log(re.test(str)); //输出true console.log(re.lastIndex); //输出14 console.log(re.test(str)); //输出ture console.log(re.lastIndex); //输出24 console.log(re.test(str)); //输出false console.log(re.lastIndex); //输出0,没有找到匹配项被重置
Above we have seen the impact of /g mode on the RegExp.test() function, now let’s take a look Impact on RegExp.exec() function. RegExp.exec() returns an array containing matching results. If no match is found, the return value is null.
var str = "012345678901234567890123456789"; var re = /abc/; //exec没有找到匹配 console.log(re.exec(str));//null console.log(re.lastIndex);//0
var str = "012345678901234567890123456789"; var re = /123/; var resultArray = re.exec(str); console.log(resultArray[0]);//匹配结果123 console.log(resultArray.input);//目标字符串str console.log(resultArray.index);//匹配结果在原始字符串str中的索引
For the RegExp.exec method, if g is not added, only the first match will be returned, no matter how many times it is executed; if g is added, then The first execution also returns the first match, the second execution returns the second match, and so on.
var str = "012345678901234567890123456789"; var re = /123/; var globalre = /123/g; //非全局匹配 var resultArray = re.exec(str); console.log(resultArray[0]);//输出123 console.log(resultArray.index);//输出1 console.log(globalre.lastIndex);//输出0 var resultArray = re.exec(str); console.log(resultArray[0]);//输出123 console.log(resultArray.index);//输出1 console.log(globalre.lastIndex);//输出0 //全局匹配 var resultArray = globalre.exec(str); console.log(resultArray[0]);//输出123 console.log(resultArray.index);//输出1 console.log(globalre.lastIndex);//输出4 var resultArray = globalre.exec(str); console.log(resultArray[0]);//输出123 console.log(resultArray.index);//输出11 console.log(globalre.lastIndex);//输出14
When using the /g matching pattern, we can get all the matches through a loop.
var str = "012345678901234567890123456789"; var globalre = /123/g; //循环遍历,获取所有匹配 var result = null; while ((result = globalre.exec(str)) != null) { console.log(result[0]); console.log(globalre.lastIndex); }
I hope this article will be helpful to everyone in JavaScript programming.
For more articles related to usage analysis of JS regular expression modifier global(/g), please pay attention to the PHP Chinese website!