I have a javascript like:
<script type="text/javascript">
function getHighlight() {
var MainText = '';
var toSearch = 'store';
MainText = MainText + '<table>';
MainText = MainText + '<tr><td><b>Stores</b></td></tr>';
MainText = MainText + '<tr><td class="clsUrl"><a href="check%20Store%20quality.aspx" style="color:blue; text-decoration:none;" target="_blank">check%20Store%20quality.aspx</a></td></tr>';
MainText = MainText + '<tr><td><p>Some Description about store...</p><br /></td></tr>';
MainText = MainText + '</table>';
$('.dv_searchResult').append(MainText);
textHightLight($(".dv_searchResult").html(), toSearch, ".dv_searchResult");
}
function textHightLight(totText, term, dvCls) {
var src_str = totText;
term = term.replace(/(\s+)/, "(<[^>]+>)*$1(<[^>]+>)*");
var pattern = new RegExp("(" + term + ")", "gi");
src_str = src_str.replace(pattern, "<mark>$1</mark>");
src_str = src_str.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/, "$1</mark>$2<mark>$4");
$(dvCls).html(src_str);
}
</script>
Here I am binding the complete html into a div dv_searchResult and then highlighting the toSearch text inside the div. Its working fine to an extent. The only problem is that if the toSearch text contains any word which exists also in url (eg, in the above text, store exists inside href="check%20Store%20quality.aspx" also.),then the url is also replaced with <mark></mark> that i am using to highlight the word. I need help on how can I ignore the clsUrl class or the <a href=""> tag while reading the text in textHightLight function.
See the example here: http://ift.tt/1Icl0ba
Kindly help.
Aucun commentaire:
Enregistrer un commentaire