javascript统计一个字符在一段字符串出现次数

时间:2024-12-14 20:34:08

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>javascript统计</title>
</head>
<body>

<input type="submit" name="dd" value="点击" onclick="show()">
<script>
function show(){

var a="123456789123456789";
var b="123";
// alert(a);
var count=0;
// alert(count);
var i=0;
for(i=0;i<=a.length;i++){

if(a.indexOf(b)==i){
a=a.substring(i+1,a.length);
count++;
}
}
alert(count);
}

</script>

</body>
</html>