如何用html标签替换匹配模式?

时间:2013-11-26 15:02:58

标签: javascript regex

我有一些HTML代码

<span style="color:#c5c5c5;">
 This is sample text </span> with ^(upper) text 
 and some ^(trademark) ^(copyright) symbols !

现在我正在使用javascript将所有字符串开头替换为^(并以...结尾)

这是我想要的结果

<span style="color:#c5c5c5;">This is sample text </span> with <sup>upper</sup> text and some <sup>trademark</sup> <sup>copyright</sup> symbols !

我正在使用函数和正则表达式来替换匹配patterm的所有字符串

Javascript在这里!

var pattern = /\^\(.*\)/;

但我不知道如何使用while()来替换我想要的所有内容:-s

1 个答案:

答案 0 :(得分:0)

@Sniffer走在正确的轨道上。我相信你要找的是

sampleText.replace(/\^\((.*?)\)/g, "<sup>$1</sup>")

或纳入@ MaxArt的建议,

sampleText.replace(/\^\(([^)]*)\)/g, "<sup>$1</sup>")