Java Script - match函数new RegExp 与 eval构造正则表达式
Java Script 代码#1
var str = "Abcdeftsadfasdfasdfasdf";
var mstr = "ABC";
var n = 0;
for (var i=0; i < 1000000; i++){
if (str.match(eval ("/" + mstr + "/i") )){
n++;
}
}
Java Script 代码#2
var str = "Abcdeftsadfasdfasdfasdf";
var mstr = "ABC";
var n = 0;
var reg = new RegExp(mstr, "i");
for (var i=0; i < 1000000; i++){
if (str.match(reg)){
n++;
}
}
两段代码,功能一样,但是使用new RegExp的代码#2会更有效的节省时间,运行速度快上100倍;