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.

1

1 Answer 1

15

http://jsfiddle.net/Aku4y/

var t = $(source).html();
t = t.replace(/04/g, "<span class='highlight'>04</span>");
$(source).html(t);
Sign up to request clarification or add additional context in comments.

1 Comment

This answer only works for this case; for a better solution you should use t = t.replace(/04/g, "<span class='highlight'>$&</span>"); instead. This way it works for any text.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.