发布网友
共3个回答
热心网友
<script language="javascript">
function contains(stringA,str,op_type)
{
/*
* N,N,N,N,N represents you set op_type = false , and we have different items returned
* N>0 represents contains
* N==0 represents no item contains
* N==-1 represents <str> is longger than <stringA>
* N==-2 represents <str> 's length is 0
* N==-3 represents special no item contains
* N==-4 when you set the op_type = true , return only the first index of <str> shows
* in this case if no item contains return -4
*/
if(str.length==0)
{
return -2;
}
if (stringA.length < str.length)
{
return -1;
}
if(stringA.indexOf(str)!=-1)
{
//alert(stringA);
/*alert(stringA.length);
alert(str);
alert(str.length);*/
//define an array to store the index+1 of which that contains the variable <str>
var mark_contain=new Array(stringA.length-str.length+1);
for(j=0;j<mark_contain.length;j++)
{
mark_contain[j]=0;
//alert(mark_contain[j]);
}
//stored a value -3 or 0 , -3 represents there is no <str> in <stringA>
var mark_contain_=0;
var returnStr="";
//alert(returnStr);
for(i=0;i<stringA.length - str.length+1;i++)
{
//alert(i);
//alert(stringA.length - str.length);
if((stringA.substring(i,str.length+i)+"")==(str+""))
{
//alert(i+1);
mark_contain[i]=i+1;
}
else
{
mark_contain_=-3;
}
}
//alert(op_type);
if(op_type)
{
//alert(mark_contain.length);
//true represents that return the first index of <str> shows
for(l=0;l<mark_contain.length;l++)
{
if(mark_contain[l]==l+1)
{
return l+1;
}
}
return -4;
}
else
{
}
//use one varia to return
//view all the elements in mark_contain[]
//check if there is some which contains no 0 item
//if so contact the str and return
//if not return mark_contain_
for(k=0;k<mark_contain.length;k++)
{
//alert(returnStr+" returnStr");
//alert(k+" k");
//check if mark_contain[k] is 0 or not
if(mark_contain[k]!=0)
{ //alert(mark_contain[k]+" mark_contain[k]");
//in this case contact
if(returnStr=="")
{
returnStr=returnStr+""+mark_contain[k];
}
else
{
returnStr=returnStr+","+mark_contain[k];
}
//alert(returnStr);
}
}
//alert(returnStr+" returnStr");
//now we can check the returnStr and mark_contain_
//first check returnStr
//alert(returnStr);
if(returnStr=="")
{
//there is no <str> in <stringA>
return mark_contain_;
}
else
{
return returnStr;
}
}
else
{
return 0;
}
}
//var abc="abcdefgefgefgefgefgefg";
//var bcd="efg";
//alert(ab.indexOf(bc));
//alert(contains("abcdefg","bcd")); //2
//alert(contains("abcdefg","acd")); //0
//alert(contains("abcdefg","cde")); //3
//alert(contains("abcdefg","efg",true)); //5
//alert(contains("abcdefg","efg",false)); //5
//alert(contains("abcdefgefgefgefgefgefg","efg",true)); //5
//alert(contains(abc,bcd,false)); //5811141720
//alert(contains(abc,bcd,true)); //5,8,11,14,17,20
//alert(contains("abcdefg","0")); //0
//alert(contains("abcdefg","")); //-2
</script>
这是我之前写过的一个查询某字符串是否在另一字符串的方法
StringA 代表原字符串
str 代表要查询的字符串
op_type 是设定是否输出所有符合条件的字符串在原字符串中的位置
你自己按着自己的需要再改改就可以了
当然你不改也行,只要("5,8,11,14,17,20").split(/\W/)就可以获取一个数组,元素个数即是总共出现的次数
热心网友
<script>
var str = 'asd8as8dsa98d98';
var me = 'as';
var len = me.length;
var tmp = '';
var times = '';
for (i=0,l=str.length;i<l;i++){
tmp = str.substr(i,len);
if ( tmp != me ) continue;
times += (times==''?'':',') + i + ':' + tmp;
}
alert(times);
alert(times.split(',').length);
</script>
逐字符循环被查找串
截取与被查串等长的字符
热心网友
下面的那个太麻烦了