Home  >  Article  >  Web Front-end  >  js function implementation method to determine the largest substring of two strings

js function implementation method to determine the largest substring of two strings

高洛峰
高洛峰Original
2016-12-08 13:08:031211browse

As shown below:

<!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>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn