无法在正则表达式中为span创建类名

时间:2017-02-21 11:53:16

标签: javascript html regex typescript

当我尝试为span元素创建类名时,会收到错误。

 var regex = new RegExp("(" + input + ")", "im");
 str = str.replace(regex, ("<span class="name">$1</span>"));        

1 个答案:

答案 0 :(得分:1)

退出中的字符

var input = 'i';
var str = 'kaixo';
var pattern = new RegExp("(" + input + ")", "gim");
str = str.replace(pattern, ("<span class=\"target\">$1</span>"));

console.log(str);

输出:

ka<span class="target">i</span>xo