RegExp.exec和String.match深入理解

时间:2023-12-05 20:44:44

今天在重新阅读《JavaScript权威指南》的RegExp和String的时候,看到了2个比较容易混淆的函数:RegExp的exec和String的match

这2个函数都是从指定的字符串中提取符合条件的字串。他们大部分时候的返回结构是一致的,但是在全局检索时,返回的结果差别会很大
1、正则表达式为非全局检索时,2者是等价的

返回符合条件的第一个子串以及对应的分组(如果存在存在)构成的伪数组:第一个元素是匹配的字串,余下的为匹配的分组。
伪数组包含2个属性:
input 返回要检索的字符串
index 返回匹配的子串在字符串的位置,在非全局检索时始终为0

example

 var testString = 'Hello World',
reg1 = /\b\w+\b/; var result1 = reg1.exec(testString); console.log(result1);//[hello]
console.log(reg1.lastIndex); //
console.log(result1.index); // var testString = 'Hello World',
reg1 = /\b(\w)+\b/; var result1 = reg1.exec(testString); console.log(result1);//[hello, o]
console.log(reg1.lastIndex); //
console.log(result1.index); //0

2、正则表达式为全局检索时,2者的处理机制和返回结构不同

exec处理机制

当正则表达式为全局检索时,exec会记录每次匹配开始、结束的位置。匹配结束位置存储在正则表达式的lastIndex中,匹配开始位置存储在结果的index中。当exec继续被调用时,正则表达式会从lastIndex进行新的匹配,并返回新的符合条件的子串及分组,并更新正则表达式的lastIndex。重复这样的操作,直到全部匹配完(exec返回null)。此时正则表达式的lastIndex为0。

match处理机制

match在要匹配的正则表达式为全局检索时,会直接返回字符串中符合条件的子串数组(不包含分组,即使存在分组)。

example

 var testString = 'Hello World',
reg1 = \b(\w+)\b, result; while((result=reg1.exec(testString)) != null){
console.log(result);
console.log(result.index,'-',reg1.lastIndex);
}
//["Hello", "Hello", index: 0, input: "Hello World"]
//0 "-" 5
//["World", "World", index: 6, input: "Hello World"]
//6 "-" 11 result = testString.match(reg1);
console.log(result);
console.log(result instanceOf Array);
//["Hello", "World"] //result instanceof Array
: true