实现一个字符串匹配算法,从字符串 S 中,查找是否存在字符串 T,若存在返回所在位置,不存在返回-1!(如果不能基于indexOf/includes等内置的方法,你会如何处理呢?)
方法一, arr.substr()遍历匹配
let S = 'adfasfsdgergvdfbewdfbbdbs';
let T = 'bbdbs';
~function(){
function myIndexOf(T){
let len = this.length;
let len2 = T.length;
let res = -1;
if(len2 > len){
return -1;
}
for(let i = 0 ; i <= len - len2; i++){
if(this.substr(i,len2) === T){
res = i;
break;
}
}
return res;
}
String.prototype.myIndexOf = myIndexOf;
}();
console.log(S.myIndexOf(T));
let S = 'adfasfsdgergvdfbewdfbbdbs';
let T = 'bbdbs';
~function(){
function myIndexOf(T){
let reg = new RegExp(T);
let res = reg.exec(this);
return res === null? -1: res.index;
}
String.prototype.myIndexOf = myIndexOf;
}();
console.log(S.myIndexOf(T));