So I have written this code here:
highlighter: function (item) {
var parts = this.query.split(" ");
var length = parts.length;
for (var i = 0; i < length; i++){
if(parts[i] != ""){
item = item.replace(new RegExp('(' + parts[i] + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>'
})
}
}
return item;
}
What it does is:
- I have the string
item, and the stringthis.query - I split
this.queryat each space, and put the resulting substrings intoparts[]
My goal is to make every occurrence of a substring from parts[] in item bold.
So if
item = "This is some text"
and
this.query = "This some"
I want <strong>This</strong> is <strong>some</strong> text.
This works perfectly, except when I get matches in the <strong> element itself. So I want only the matches replaced that aren't in the strong tag itself. Because I get resulting strings with ong> or trong>in it. Is this possible?
var parts = this.query.split(" "). There's no point constructing an array only to discard it immediately.