3
我試圖在包含@char的單詞周圍添加span標籤+ classname。在字符串內部添加標籤+類名
我目前的一段代碼只會返回一個真正的值,如果它被發現,不幸的是我不知道如何添加標籤+ classname。什麼是正確的方法來做到這一點?
var string = "hello @peter how are you?", substring = "@";
if (string.indexOf(substring) > -1){
//add span tag + classname around @peter
}
endresult應變量改成這樣:
var string = "hello <span class='mentioned'>@peter</span> how are you?"
其他[**託尼小馬**](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self -contained-tags#answer-1732454),這似乎是[** this **]的副本(http://stackoverflow.com/questions/12824928/jquery-wrapping-a-string-that-matches-a -regex) – adeneo