因为javascript无需编译可直接在
D4KPHA+t7bA/dbQtcSx6cD6sunV0sv509DX1rf7tK48YnI+CjwvcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20131205/20131205131352484.jpg" alt="">
执行结果:
代码:
function search(src,find,start){ ////// 字符串匹配(KMP) /// /// ///返回匹配字符串的位置,不存在则返回-1 if (typeof start !== "number" || start <0) { start = 0; } var lenSrc = src.length; var lenFind = find.length; if(lenFind<=lenSrc){ src = src.split(""); find = find.split(""); var temp; lenSrc-=lenFind-1; for (var i = start; i < lenSrc; i++) { //初始化临时temp temp = 0; for(var j=0;j0){ if(charSrc==src[i + temp]){ temp +=1; } else{ temp=0; } }else{ temp=0; } }else{ if(temp>0){ //console.log 便于查看结果 console.log("start:" + i + " jump to " + (i + j - temp) + " step " + (j - temp)); //-1是因为 for循环下次会+1 i+=(j - temp - 1); } break; } } } } return -1;};