Searching Text and Highlighting using jQuery

In this small blog, I will show you how can we highlight the search pattern for the length paragraph. I have seen a lot of people manually writting their own logic to highlight the text. But here I will show you using regular expression.
 
Copy the below html
  1. <input type="text" id="search" placeholder="EnterSearch Text" /><br/>  
  2. <div id="txtdesc" rows="40" cols="40" style="border:1px solid black">  
  3.         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,   
  4.         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   
  5.         typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with   
  6.         desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.  
  7. </div>  
  8. <br/>  
  9. <button id="btn">Search</button>  
In the above code, I have taken a textbox to enter the search pattern. This pattern will be searched from the div section. A button, on click of which the entered pattern should be higlighted.
 
Now copy the below JavaScript. 
  1. <script type="text/javascript">  
  2.    
  3.   $('#btn').bind('click'function (e) {    
  4.             e.preventDefault();    
  5.             var searchTxtBox = $('#search');    
  6.             searchTxtBox.val(searchTxtBox.val().replace(/(\s+)/,"(<[^>]+>)*$1(<[^>]+>)*"));    
  7.             var textarea = $('#txtdesc');    
  8.             var enew = '';    
  9.             if (searchTxtBox.val() != '') {    
  10.     
  11.                 enew = textarea.html().replace(/(<mark>|<\/mark>)/igm, "");    
  12.                 textarea.html(enew);     
  13.                     
  14.                 var query = new RegExp("("+searchTxtBox.val()+")""gim");    
  15.                 newtext= textarea.html().replace(query, "<mark>$1</mark>");    
  16.                 newtext= newtext.replace(/(<mark>[^<>]*)((<[^>]+>)+)([^<>]*<\/mark>)/,"</mark><mark>");    
  17.     
  18.                 textarea.html(newtext);     
  19.   
  20.             }    
  21.             else {    
  22.                 enew = textarea.html().replace(/(<mark>|<\/mark>)/igm, "");    
  23.                 textarea.html(enew);     
  24.             }    
  25.         });     
  26. </script>    
In the above JavaScript, I have implemented the button click event. Actually what is done here is the entered text pattern is searched and replaced with <mark/> tag. <mark/> tag is a html 5 element used to highlight the text. Thats it.
 
Output:

 
The above used regular expression will search the pattern not only in the word but also as a partial text irrespective of case(either upper or lower case). In the below code, if the second parameter to Regex() class is passed as "im" only then, only the first occurance of search pattern is checked & hightlighted
  1. var query = new RegExp("("+searchTxtBox.val()+")", "im");       
Conclusion

Hope this small code snippet will help you for achieving highlighting text using jQuery.