var s = "The rain in Spain falls rain mainly in the rain plain";
比如rain这个单词,怎么查出来它出现了三次啊...
18 个解决方案
#1
var s = "The rain in Spain falls rain mainly in the rain plain";
var re = new RegExp("rain","g");
var arr = s.match(re);
alert(arr.length)
#2
var s = "The rain in Spain falls rain mainly in the rain plain";
var reg=/rain/g;
var i=0;
while(reg.test(s)){
i++;
}
alert(i);
var reg=/rain/g;
var i=0;
while(reg.test(s)){
i++;
}
alert(i);
#3
<script>
var s = "The rain in Spain falls rain mainly in the rain plain";
var reg = new RegExp("rain", "g");
alert(s.match(reg).length);
</script>
#4
晕,没刷新,导致没看到上面的回复
居然和1楼的一模一样。。。
居然和1楼的一模一样。。。
#5
match(regExp)
返回一个数组
返回一个数组
#6
var s = "The rain in Spain falls rain mainly in the rain plain";
var reg=/rain/g;
var i=0;
while(reg.test(s)){
i++;
}
alert(i);
#7
我给个很没意思的解法。
<script>
var s = "The rain in Spain falls rain mainly in the rain plain";
s = " " + s + " ";
alert( s.split('rain').length -1 );
</script>
<script>
var s = "The rain in Spain falls rain mainly in the rain plain";
s = " " + s + " ";
alert( s.split('rain').length -1 );
</script>
#8
我总是这样写正则表达式:
var s = "The rain in Spain falls rain mainly in the rain plain";
var len = s.match(/rain/g); //正则表达式
alert(len.length);
#9
谢大家,惭愧中。。。
我好菜,要努力了...
我好菜,要努力了...
#10
我太菜了...
#11
alert("The rain in Spain falls rain mainly in the rain plain".split(/\brain\b/).length - 1);
#12
]
var s = "The{|*se*|} rain {|*se*|} in Spain {|*se*|} falls rain mainly in the rain plain";
但是我找{|*se*|}的话为什么出现数量词错误这样的提示啊
#13
顶
#14
var s = "The{|*se*|} rain {|*se*|} in Spain {|*se*|} falls rain mainly in the rain plain";
var re = new RegExp("{|*se*|}","g");
alert(s.match(reg).length);
这样报数量词错误呃
#15
学习一下!
#16
转义*号和|号~
var s = "The{|*se*|} rain {|*se*|} in Spain {|*se*|} falls rain mainly in the rain plain";
alert(s.match(/{\|\*se\*\|}/ig).length)
#17
用这个方法,要学学正则表达式
解决办法如16楼
#18
谢谢,我会好好学的...
#1
var s = "The rain in Spain falls rain mainly in the rain plain";
var re = new RegExp("rain","g");
var arr = s.match(re);
alert(arr.length)
#2
var s = "The rain in Spain falls rain mainly in the rain plain";
var reg=/rain/g;
var i=0;
while(reg.test(s)){
i++;
}
alert(i);
var reg=/rain/g;
var i=0;
while(reg.test(s)){
i++;
}
alert(i);
#3
<script>
var s = "The rain in Spain falls rain mainly in the rain plain";
var reg = new RegExp("rain", "g");
alert(s.match(reg).length);
</script>
#4
晕,没刷新,导致没看到上面的回复
居然和1楼的一模一样。。。
居然和1楼的一模一样。。。
#5
match(regExp)
返回一个数组
返回一个数组
#6
var s = "The rain in Spain falls rain mainly in the rain plain";
var reg=/rain/g;
var i=0;
while(reg.test(s)){
i++;
}
alert(i);
#7
我给个很没意思的解法。
<script>
var s = "The rain in Spain falls rain mainly in the rain plain";
s = " " + s + " ";
alert( s.split('rain').length -1 );
</script>
<script>
var s = "The rain in Spain falls rain mainly in the rain plain";
s = " " + s + " ";
alert( s.split('rain').length -1 );
</script>
#8
我总是这样写正则表达式:
var s = "The rain in Spain falls rain mainly in the rain plain";
var len = s.match(/rain/g); //正则表达式
alert(len.length);
#9
谢大家,惭愧中。。。
我好菜,要努力了...
我好菜,要努力了...
#10
我太菜了...
#11
alert("The rain in Spain falls rain mainly in the rain plain".split(/\brain\b/).length - 1);
#12
]
var s = "The{|*se*|} rain {|*se*|} in Spain {|*se*|} falls rain mainly in the rain plain";
但是我找{|*se*|}的话为什么出现数量词错误这样的提示啊
#13
顶
#14
var s = "The{|*se*|} rain {|*se*|} in Spain {|*se*|} falls rain mainly in the rain plain";
var re = new RegExp("{|*se*|}","g");
alert(s.match(reg).length);
这样报数量词错误呃
#15
学习一下!
#16
转义*号和|号~
var s = "The{|*se*|} rain {|*se*|} in Spain {|*se*|} falls rain mainly in the rain plain";
alert(s.match(/{\|\*se\*\|}/ig).length)
#17
用这个方法,要学学正则表达式
解决办法如16楼
#18
谢谢,我会好好学的...