Regex or jquery replace all string matches in another string

This question already has an answer here:

  • How do you use a variable in a regular expression? 16 answers

  • 使用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模式中使用变量?

    下一篇: 正则表达式或jQuery替换另一个字符串中的所有字符串匹配