替换不在HTML标记内的子字符串

时间:2016-02-02 13:04:38

标签: javascript regex

var token = "test123123{parent-area-identifier}<span data-tokenval='{=percent?metricType=1&area={parent-area-identifier}&with=E92000001&period=cal_2012}' class='mceNonEditable'>{=percent?metricType=1&area={parent-area-identifier}&with=E92000001&period=cal_2012}<span class='removetoken' href='javascript:;'>X</span></span>"

在上面的令牌字符串中,我想替换"{parent-area-identifier}",但不是<span>标记内的字符串匹配

所以,如果我用"ZZZ"替换它,输出应该是: "test123123 ZZZ<span data-tokenval='{=percent?metricType=1&area={parent-area-identifier}&with=E92000001&period=cal_2012}' class='mceNonEditable'>{=percent?metricType=1&area={parent-area-identifier}&with=E92000001&period=cal_2012}<span class='removetoken' href='javascript:;'>X</span></span>"

1 个答案:

答案 0 :(得分:0)

token.replace(/{parent\-area\-identifier}/, "ZZZ")

这将匹配{parent-area-identifier}的第一次出现,并将其替换为“ZZZ”字符串。请注意,反向斜杠用于转义短划线字符。