Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight matching text in javascript [duplicate]

Tags:

javascript

Possible Duplicate:
Highlight text range using JavaScript
Find text string in jQuery and make it bold

Supposing I have a bunch of file paths, similar to:

  • /Volumes/A//Array/05_SCRIPTS/staging/tree/04_OUTPUTS/MPEG_FF_PS_wCropping/01_NBC_Youtube/Backlog/Archive/LawAndOrder_S20

  • /Volumes/A//AArray/05_SCRIPTS/staging/tree/04_OUTPUTS/MPEG_FF_PS_wCropping/01_NBC_Youtube/Backlog/Archive/LawAndOrder_S20/nbcnetwork-pds-E8818-US-insert-20121020

How would I highlight all matches of specific text on the page. For example, if "04" was what I was looking for, it would do something like:

$(source).find('04').addClass('highlight');

This is pseudocode that is probably way off the mark, but this is what I'm looking to do.

like image 575
David542 Avatar asked Jan 25 '13 01:01

David542


1 Answers

http://jsfiddle.net/Aku4y/

var t = $(source).html();
t = t.replace(/04/g, "<span class='highlight'>04</span>");
$(source).html(t);
like image 185
Edmund Moshammer Avatar answered Nov 04 '22 07:11

Edmund Moshammer