// 蛮力字符串匹配
const source = 'NOBODY-NOTICED_HIMI'
const str = 'NOTIC' // want to get six
/**
* 第一个匹配子串最左的下标
*/
function stringMatching(text,pattern){
const textLength = text.length;
const patternLength = pattern.length;
for (let i = 0; i <= textLength - patternLength; i++) {
let j;
for (j = 0; j < patternLength; j++) {
console.log(text[i+j],pattern[j])
if (text[i + j] !== pattern[j]) {
break;
}
}
console.log("循环结束")
if (j === patternLength) {
return i;
}
}
return -1;
}
console.log(stringMatching(source,str))