[英]AngularsJS JavaScript Matching Words and Adding String
我在contentString
有一個長文本。
在$scope.listKeyWords
,我有一些關鍵字(car,house,red,cool)
我需要在文本中匹配此關鍵字,並添加一些內容,例如:
輸入 contentString
:
Hello there, I have a very nice car. It is very cool.
預期輸出:
Hello there, I have a very nice <u>car</u>. It is very <u>cool</u>.
[編輯] [解決]感謝@Barth Zalewski
for (var k = 0, word; word = $scope.listKeyWords[k]; k++) {
var re = new RegExp(word, 'g');
contentString = contentString.replace(re, "<u>" + word + "</u>");
}
我該如何進行?
謝謝你的幫助
for (var k = 0, word; word = $scope.listKeyWords[k]; k++) {
contentString = contentString.replace(new RegExp("/\\b" + word + "\\b/"), "<u>" + word + "</u>");
}
\\b
表示“單詞邊界”,因此僅替換整個單詞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.