javascript - highlight text in a variable using RegExp and span -
i have div of text. want highlight of text stored in variable using regexp , span. text highlighted unknown @ run time.
var t = $("#highlightoutput").html(); //sentence var s = t.search(hstr);//what shd replaced var query = new regexp(s, "g"); console.log(s); t = t.replace(query, "<span class='highlight'>"+s+ "</span>"); //problem here //how write regex code highlight text stored in s. $("#highlightoutput").html(t);
search
returns index, want actual text. use match
that: method returns array of matches, , can take first one.
var s = t.match(hstr)[0];
Comments
Post a Comment