Replace hightlighted word in span tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anitha111
    New Member
    • May 2014
    • 1

    #1

    Replace hightlighted word in span tag

    Code:
    console.log(t);
    console.log('temp[1]='+temp[1]);
      res = document.getElementById("labnol");
    
    //var d = document.getElementsByTagName("body")[0];

    Hey i ve wriiten code for hightighting a particular word in javascript.But m not getting how to replace all the hightighted word with dynamic words.
    This is the code have written for highlighting words.

    Code:
    if ( t >= 0)
    {
       var re = new RegExp("(\\b" + temp[1] + "\\b)", "gim");//pattern for keyword
    console.log(re);
    
      // var re0 = new RegExp("[>][^><]*[><]", "gi");//pattern to get textnode
    
    
       res.innerHTML = res.innerHTML.replace(re, function (text) {
           
            //with each textNode, looking for keyword
          return text.replace(re, "<span class=\"search-result\" style=\"background-color:yellow;\">$&</span>");
    }
    Last edited by Dormilich; May 21 '14, 07:31 AM. Reason: please use [CODE] [/CODE] tags when posting code
Working...