7

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.

Community
  • 1
  • 1
David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    This might help http://stackoverflow.com/questions/9794851/find-text-string-in-jquery-and-make-it-bold/9795091#9795091 – elclanrs Jan 25 '13 at 01:17

1 Answers1

15

http://jsfiddle.net/Aku4y/

var t = $(source).html();
t = t.replace(/04/g, "<span class='highlight'>04</span>");
$(source).html(t);
Edmund Moshammer
  • 688
  • 7
  • 19
  • 11
    This answer only works for this case; for a better solution you should use `t = t.replace(/04/g, "$&");` instead. This way it works for any text. – JoseHdez_2 Sep 16 '18 at 11:08