0
我想根據數組中的預定義單詞列表搜索網頁正文的文本。然後記錄每個單詞有多少個實例出現。使用數組的Javascript搜索字符串
<script>
var str = document.body.innerText;
var array = [];
array[0] = "Statement";
array[1] = "Documents";
var regexp = new RegExp('\\b' + array.join('\\b|\\b') + '\\b').test(str);
document.write(regexp)
</script>
那一個只返回真或假。 或者有沒有辦法讓這個工作?
var str = document.body.innerText;
var n = str.match(use array here)
document.write(n)
TRY(這裏使用陣列)做'VAR N = str.match。長度;' – Safirah
還檢查了這樣一個問題: http://stackoverflow.com/questions/1072765/count-number-of-matches-of-a-regex-in-javascript – Safirah
不要使用非標準的'innerText'。使用'textContent'。 – Oriol