找出数组下标并返回下标数
代码演示:
<script>
var str = 'Hello world, Hello javascript.';
var newArr = getChar(str,'o');
document.write(newArr);
function getChar(str,myChar) {
var arr = [];
var index = str.indexOf(myChar);
arr.push(index);
while (index != -1) {
index = str.indexOf(myChar,index + 1);
if(index != -1) {
arr.push(index);
}
}
return arr;
}
</script>
输出结果:
4,7,17