以下に示すように:
<!DOCTYPE html> <html> <head> <title></title> </head> <script type="text/javascript"> function search(str1,str2) { var i=j=k=a=jk=kk=0; var m=str1.length; var n=str2.length; var index=0; var maxlen=0; var str=""; var flag=0; for (i=0;i<n;i++) { k=i; for (j=0;j<m;j++) { a = 0; flag = 0; jk=j; kk=k; while(jk<=m&&str1[kk]==str2[jk]) { kk++;jk++;a++; flag = 1; } if(flag==1) { if(a>maxlen) { maxlen = a; index = jk-a;//匹配的首项 } } } } for(;maxlen>0;maxlen--) { str+=str2[index++]; } return str; } alert(search("kssd","ssdfa")); </script> <body> <div>11</div> </body> </html>