JavaScript怪癖和秘密之动态正则表达式实例页面

代码:

HTML代码:
<button id="button">点击弹出结果</button>
                
JS代码:
function findWord(word, string) {
    var instancesOfWord = string.match(new RegExp('\\b'+ word +'\\b', 'ig'));
    alert(instancesOfWord);
}
document.getElementById("button").onclick = function() {
    findWord('car', 'Carl went to buy a car but had forgotten his credit card.');
};
                

效果: