在昨天开发中,使用正则表达式。代码如下: var start = +new Date(), end, globalRegex = /(\\r\\n)|(\\n\\r)|(\\r)|(\\n)+/g, nonGlobalRegex = /\\r\\n|\\n\\r|\\r|\\n+/g, testStr = 'This optimization makes the lexer more\r\n than twice as fast! Why does this make sense?\r\n First, if you think about it in the simplest way possible, the iteration over rules moved from\r\n Python cod'; for (var i = 10000; i >= 0; i--) { globalRegex.test(testStr); } end = +new Date(); console.log("=----------------匹配使用时间:"+(end - start)); 发现不加小括号的会比加小括号的时间短,有点想不明白为什么,而且相差的还挺多的。
在昨天开发中,使用正则表达式。代码如下:
var start = +new Date(),
end,
globalRegex = /(\r\n)|(\n\r)|(\r)|(\n)+/g,
nonGlobalRegex = /\r\n|\n\r|\r|\n+/g,
testStr = ‘This optimization makes the lexer more\r\n than twice as fast! Why does this make sense?\r\n First, if you think about it in the simplest way possible, the iteration over rules moved from\r\n Python cod’;
for (var i = 10000; i >= 0; i–) {
globalRegex.test(testStr);
}
end = +new Date();
console.log("=----------------匹配使用时间:"+(end - start));
发现不加小括号的会比加小括号的时间短,有点想不明白为什么,而且相差的还挺多的。
在昨天开发中,使用正则表达式。代码如下:
var start = +new Date(),
end,
globalRegex = /(\r\n)|(\n\r)|(\r)|(\n)+/g,
nonGlobalRegex = /\r\n|\n\r|\r|\n+/g,
testStr = ‘This optimization makes the lexer more\r\n than twice as fast! Why does this make sense?\r\n First, if you think about it in the simplest way possible, the iteration over rules moved from\r\n Python cod’;
for (var i = 10000; i >= 0; i–) {
globalRegex.test(testStr);
}
end = +new Date();
console.log("=----------------匹配使用时间:"+(end - start));
发现不加小括号的会比加小括号的时间短,有点想不明白为什么,而且相差的还挺多的。
this is good question!

其实这不是一个前端的问题
这是一个正则表达式的问题,重点在于小括号的在正则的定义,小括号不光是提高优先级的,还有一个作用是捕获符号,也就是说这个内容需要单独捕获单独记录.所以因为这个捕获机制的存在导致效率有一定的影响,你也可以通过match方法甚至是$1-$9来获取部分捕获内容
this is good question!

其实这不是一个前端的问题
这是一个正则表达式的问题,重点在于小括号的在正则的定义,小括号不光是提高优先级的,还有一个作用是捕获符号,也就是说这个内容需要单独捕获单独记录.所以因为这个捕获机制的存在导致效率有一定的影响,你也可以通过match方法甚至是$1-$9来获取部分捕获内容