I am new to webDevelopment
. I have string which has some text.Now I want to highlight some words from that text file . So, Here I am using this logic
$scope.highlightHTML = function (content,startoffset,endoffset,color) {
var className = 'mark';
console.log(content.substring(startoffset, endoffset));
return content.replace(content.substring(startoffset, endoffset), '<span class="' + className + '">$&</span>');
}
Now this is working fine. But Now what happens is when first word gets highlighted and then when It tries to highlight the second word then the strings offsets are getting changed because of this replacing . It takes tag as well so, now offsets are getting changed. now when I highlight some text then next time it should not take offsets of start and end offset of the span tag . SO, How can I do this ?
Its like , "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged"
I have this string. Now, I want to highlight when an unknown printer took a galley of
Now, for this I use substring because I get the start and end from back-end itself. I replace only that string with mark tag.
Now The problem is after this immediately,If I want to highlight but also the leap into electronic typesetting
, Then the offset which I got from the backend will not be useful because while replacing the first string I added span tag, so it is taking the span tag offsets as well. So, Its not getting me the exact string by providing the offsets as well. It gives me some another string because offsets has changed. Now, whil highlighting the offsets should not get changed by adding the span tag.
Ajax call -
jsonDataArray.forEach(function (item) {
responseData = $scope.highlightHTML(responseData,item.startOffset,item.endOffset,item.color);
$rootScope.data.htmlDocument = responseData.replace(/
/g, "</br>");;
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…