Regular expression global matching mode

First of all, to be clear, all regular expressions have an lastIndexattribute to record the position where the last match ended. If it is not a global matching mode, lastIndexthe value is always 0, and after a match, the matching will stop.

The global matching mode of regular expressions is to use the gidentifier or set the globalattribute 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 lastIndexattribute 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 NULLor returns an array. The first 0element of the array stores the strmatching content found in the string , 1and the nlast 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 0to ncontains all of the matched entry does not contain sub-match. At this time, you can use to RegExp.$1..$9get a 9submatch.


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 lastIndexattribute 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 lastIndexto0

let reg = /^[\d]{2}$/g;
	let str = "12";
	console.log(reg.test(str));     //返回true
	let = "123";
	console.log(reg.test(str));     //从数字3开始匹配,只有一个数字,故返回false

Reset lastIndexto0

 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 forEachmethod to testtest the strings in the array , there will be flase truealternate appearance, falsewhether trueit 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
    });
 

Guess you like

Origin blog.csdn.net/WuLex/article/details/109024684