Reputation: 1239
I have a long text in contentString
.
In $scope.listKeyWords
, I have some keywords (car, house, red, cool)
I need to match this keywords in my text and add some stuff like:
Input contentString
:
Hello there, I have a very nice car. It is very cool.
Output expected:
Hello there, I have a very nice <u>car</u>. It is very <u>cool</u>.
[EDITED] [SOLUTION] thanks to @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>");
}
How can I proceed?
Thanks for your help
Upvotes: 1
Views: 176
Reputation: 3819
for (var k = 0, word; word = $scope.listKeyWords[k]; k++) {
contentString = contentString.replace(new RegExp("/\\b" + word + "\\b/"), "<u>" + word + "</u>");
}
The \b
denotes a "word boundary" so that only whole words will be replaced.
Upvotes: 2