2013-11-26 32 views
1

我有以下如何用html標籤替換匹配模式?

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

現在我使用JavaScript來代替所有的字符串開始一些HTML代碼^(和結束)

這是結果,我想

<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 Here!

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

但我不知道使用一段時間(),以取代所有我想如何:-s

+0

你或許應該使用非貪婪匹配。甚至這個:/ \^\\([^)] * \\)/ –

回答

0

@Sniffer是在正確的軌道上。我相信你要找的是什麼

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

或者含有@ MaxArt的建議,

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