"use strict";
String.prototype.search2 = function (re) {
var initLastIndex = re.lastIndex; //保存正则对象的初始lastIndex
re.lastIndex = 0; //将其置为0已让exec从头开始匹配
var match = re.exec(this); // 查找re里是否包含this
re.lastIndex = initLastIndex; //exec匹配完后再恢复原来的置
if (!match) { //没有找到返回其-1
return -1;
}
else {
return match.index; //找到返回数组的下标
}
};
正则表达式search的实现
最新推荐文章于 2024-08-11 18:40:36 发布