这里仅就上述特性的测试来判断JS中正则表达式的类型。
本次测试的使用的是nodejs、chrome、firefox和ie,由于这四个所表示的结果一致,所以只贴出来同一个测试结果。
1.忽略优先量词测试
var res = '123456'.match(/\d{3,5}/); console.log(res); var res = '123456'.match(/\d{3,5}?/); console.log(res);
得到的结果如下:
[ '12345', index: 0, input: '123456' ] [ '123', index: 0, input: '123456' ]
因此忽略优先量词是有效的。
2. 多选结构的测试
var res = 'nfa not'.match(/nfa|nfa not/); console.log(res);
得到结果如下:
[ 'nfa', index: 0, input: 'nfa not' ]
证明优先选择第一个分组nfa。
3. 可以使用捕获分组的括号
这里使用trim的一种实现方法举例。
String.prototype.trim = function(){ return this.replace(/^\s*(.*?)\s*/, function($1, $2){ return $2; }); };
上面代码的$2就是捕获分组的应用。
4. 对比egrep
根据对比grep -E的实现如下。
忽略优先测试如下:

分支测试如下:

因此可以得出结论,目前主流的JS引擎使用的是NFA匹配规则!