我想强调用于进行研究的词。我正在尝试在 JavaScript 中使用正则表达式,但我遇到了以下问题。
这是我正在使用的代码:
var key_word = "and";
var text= "I bought milk and water";
var re = new RegExp("(" + key_word + ")", 'ig');
text = text.replace(re, "<strong>$1</strong>");
console.log(text);
正确结果:
I bought milk <strong>and</strong> water.
但随着文字
var text = "I am getting a better understanding.";
错误结果:
I am getting a better underst<strong>and</strong>ing.