javaScript-查找字符串中所有相同字母的位置

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        var arr="helloeeaeeae";
        // 遍历每个字母
        for(var i=0; i<arr.length; i++){
            // 查找所有e的位置
            var index=arr.indexOf("e",index+1);
            if(index<0){//如果返回的是-1,则没找到
                break;
            }
            console.log(index);//输出找到的下标
        }
    </script>
</head>
<body>
    
</body>
</html>

 

posted @ 2021-03-25 15:43  Hhhr  阅读(206)  评论(0编辑  收藏  举报