查询字符串中某个字符出现的位置数组

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    var str = "bdsajsbvfcsddansfkjanhfhbfshdabajh";
    var arr = [];
    var pos = str.indexOf("a");
    while(pos>-1){
        arr.push(pos);
        pos = str.indexOf("a",pos+1);
    };
    console.log(str);
    console.log(arr);
});
</script>
</body>
</html>

 

posted on 2017-05-18 08:58  jasonduanmu  阅读(837)  评论(0编辑  收藏  举报

导航