Regex or jquery replace all string matches in another string
This question already has an answer here:
使用RegExp()
将搜索字符串转换为正则表达式
var searchedTerm = "test";
var returnedString = "National testing Plant testers";
var replacedString = returnedString.replace(new RegExp(searchedTerm, 'g'), '<span class="highlight">' + searchedTerm + '</span>');
document.write(replacedString);
.highlight {
color: red;
}
链接地址: http://www.djcxy.com/p/76824.html
上一篇: 如何在RegEx模式中使用变量?