function boldKeywords(inputString, keywords){
for (var i = 0; i < keywords.length; i++) {
var key = keywords[i];
if (key) inputString= inputString.replace(new RegExp(key, 'gi'), '<strong>' + key + '</strong>');
}
return s;
}
Using the JavaScript function above, I attempted to emphasize all occurrences of specific words in a given string based on a list of "keywords" or parts of strings.
For example:
keywords=["man", "str"]
inputString="strong man"
Desired output:
strong man
However, the current output ends up replacing the instances of keywords within existing HTML tags as well. This results in
<strong>strstrong>ong man.
Is there a regular expression or alternative method that can be used to ignore HTML tags within a string and prevent this unexpected behavior?