在第n个位置替换正则表达式只是不起作用

时间:2017-06-15 02:56:22

标签: javascript regex

我的文字是

<DIV STYLE="text-align:Left;font-family:Segoe UI;font-style:normal;font-weight:normal;font-size:12;color:#000000;"><P STYLE="font-family:Arial;margin:0 0 0 0;"><SPAN STYLE="font-family:Segoe UI;"><SPAN>{firstname} will continue to learn high frequency words, try to remember words and us them in their writing. {firstname} always uses correct spacing in their writing and attempts to use punctuation.</SPAN></SPAN></P></DIV>

所以现在我想用 {firstname_C}

替换索引 202 {firstname}

我的代码不起作用

var endIndex = 202;
var pattern = new RegExp("({firstname}{" + endIndex + "})", "g");
                commentString = commentString.replace("({firstname})/g", "{firstname_C}");

here

引用此代码

1 个答案:

答案 0 :(得分:0)

gallery.htmlcontact.html是regexp中的特殊字符,您必须以模式转义它们:

尝试:

{