2011-02-05 121 views
1

我想用HTML標籤替換符號。例如:用HTML標籤替換符號

This :is: some :text: that could also look :a: bit :different:. 

應該成爲

This <span id="selected">is</span> some <span id="selected">text</span> that could also look <span id="selected">a</span> bit <span id="selected">different</span>. 

回答

1
$str = "This :is: some :text: that could also look :a: bit :different:"; 
$str =~ s/:(\w+):/<span id='selected'>$1<\/span>/g; 
print $str; 
1
(:)(.+?)\1 

使用\1將允許你改變了 「:」 到其他字符,並確保關閉匹配。

2
$s =~ s{:([^:]+):}{<span id="selected">$1</span>}g;