First of all, to be clear, all regular expressions have an lastIndex
attribute to record the position where the last match ended. If it is not a global matching mode, lastIndex
the value is always 0, and after a match, the matching will stop.
The global matching mode of regular expressions is to use the g
identifier or set the global
attribute to when creating the regular expression true
. In the global matching mode, the regular expression will perform multiple matches on the specified string. Each match uses the lastIndex
attribute value of the current regular object as the starting position for searching in the target string. If no match is found lastIndex
, the value will be reset to 0
.
After understanding the above, the result of the following code is very clear:
var regex = /abc/g;
var str = '123#abc';
console.log(regex.lastIndex); // 0
console.log(regex.test(str)); // true
console.log(regex.lastIndex); // 7
console.log(regex.test(str)); // false
console.log(regex.lastIndex); // 0
console.log(regex.test(str)); // true
console.log(regex.lastIndex); // 7
console.log(regex.test(str)); // false
Today, I encountered a problem when writing form validation. Every time an even number of clicks occurs, an error will be reported. This problem is caused. The solution is also very simple, that is, remove the regular expression g
.
About RegExp.prototype.exec(str)
methods and String.prototype.math(rgExp)
methods:
RegExp.prototype.exec(str)
The method returns NULL
or returns an array. The first 0
element of the array stores the str
matching content found in the string , 1
and the n
last element returns the content of the submatch ()
specified in the pattern with brackets " ".
The String.prototype.math(rgExp)
method and RegExp.prototype.exec(str)
behavior are similar when the global flag is not used . When setting the global matching flag String.prototype.math(rgExp)
array element method returns the entry 0
to n
contains all of the matched entry does not contain sub-match. At this time, you can use to RegExp.$1..$9
get a 9
submatch.
js regular expression test()
method
test()
The method is a method of regular expressions, which is used to check whether a string matches a certain pattern. The
test
method checks whether the string matches the given regular expression pattern, and returns if it is true
, otherwise it returns false
.
Every regular expression has an lastIndex
attribute that is used to record where the last match ended.
grammar:regexp.test(str)
In global match mode
For the same regular object repeatedly called, the next matching position will appear, starting from the position where the last matching ended, and the solution is reset lastIndex
to0
let reg = /^[\d]{2}$/g;
let str = "12";
console.log(reg.test(str)); //返回true
let = "123";
console.log(reg.test(str)); //从数字3开始匹配,只有一个数字,故返回false
Reset lastIndex
to0
let reg = /^[\d]{2}$/g;
let str = "12";
console.log(reg.test(str)); //返回true
let = "123";
reg.lastIndex = 0;
console.log(reg.test(str)); //返回true
In non-global matching mode, this problem does not exist
let reg = /^[\d]{2}$/;
let str = "12";
console.log(reg.test(str)); //返回true
let = "123";
console.log(reg.test(str)); //返回true
In addition, the test()
method and the array forEach()
method may not achieve the expected results when used together for
. The same is true in the loop, as follows
let line = '1a 123 123 12';
let lines = line.split(' ');
let reg = /^[\d]+$/g;
let res = 0;
lines.forEach( function(element, index) {
console.log(reg.test(element)); // flase true 交替出现
});
Use the forEach
method to test
test the strings in the array , there will be flase true
alternate appearance, false
whether true
it appears first or not depends on the result of the first element of the array.
Similarly, in the non-global matching mode, this problem does not exist, and the expected result can be obtained by removing the global matching g.
let line = '1a 123 123 12'
let lines = line.split(' ');
let reg = /^[\d]+$/;
let res = 0;
lines.forEach( function(element, index) {
console.log(reg.test(element)); // false true true true
});