1. 程式人生 > >specific word count(index of)

specific word count(index of)

統計檔案中某一詞語出現次數:
HTML:
    <p id="p1">start,stop,speed,start,speed ,velocicty,start</p>
    <button onclick="fun()">Try it</button>
    <p id="demo"></p>
    <script src="./index.js"></script>

JS:
function fun(){ var str = document.getElementById("p1").innerHTML; var num=0; var a=str.indexOf("start"); while(a!==-1){ a=str.indexOf("start",a+1); num++; } document.getElementById("demo").innerHTML = num; }