2

Say I have this:

<div>
    People say <a href="apples.html">apples</a> are nice. 
    It's true, apples are nice. You go, <b>apples</b>!
    <img src="apples.jpg" />
</div>

How would I go about replacing every occurrence of "apples" (with "oranges", for example) within that div (including any tag's element content and plain text) WHILE avoiding replacing such occurrences in the href and src attributes, or any attribute for that matter.

So the end result would be:

<div>
    People say <a href="apples.html">oranges</a> are nice.
    It's true, oranges are nice. You go, <b>oranges</b>!
    <img src="apples.jpg" />
</div>

Should note, while text() would do me a good job in this example, I will be replacing with html elements so it's not particularly helpful in the actual case.

Thanks!

2
  • Is it possible that you've tried and failed? Can you show us what you've tried? Commented Apr 11, 2014 at 20:19
  • Should note, while text() would do me a good job in this example, I will be replacing with html elements so it's not gonna help me in the real thing. @Jay tried a bunch of approaches and none of them worked out as I had initially expected, I'm not thinking this through properly for some reason. Commented Apr 11, 2014 at 20:22

2 Answers 2

2

The :contains selector will help you here. Given that the div is the root of you search & replace, the following will allow you to replace apples with oranges:

$("div").find(":contains('apples')").each(function(){
    $this = $(this);

    //Example replacement using HTML, but can also just us text() for simple changes
    $this.html($("<i>").text("oranges"));
});

Based on your comments, I've shown it replacing apples with a little html ... italic oranges.

See the JSFiddle for a working example.

Update - altering source html

The comments pointed out that this missed out the "apples" in the body of the div. If you have control of the Html (I assume you do), then it makes everything easier if you can change the Html to wrap the problem "apples" in a span, so <span>apples</span>. This makes the html:

<div>
    People say <a href="apples.html">apples</a> are nice. 
    It's true, <span>apples</span> are nice. You go, <b>apples</b>!
    <img src="apples.jpg" />
</div>

Basically, markup needs to be in place to help you here. See the updated JSFiddle.

If that isn't possible, an additional Regex to catch this would be needed.

Update - can't update source html, use Regex

Ok, so altering the source html is not possible. The following solution, just uses a Regex over the html, which matches all "apples" that are followed by a "<" or a space:

$("div:contains('apples')").each(function(){
    $this = $(this);
    $this.html($this.html().replace(/apples(?=<|\s)/gi, "oranges"));
});

Working version shown in the JSFiddle

Sign up to request clarification or add additional context in comments.

5 Comments

This however doesn't replace the "apples" in the plain text bit. Another approach I attempted also ended up with the same result.
@frostyNinja True, I'd missed that bit!
Wrapping in spans is out of the question, because this ties in with a blog with hundreds of occurrences of strings that I need replaced. The whole deal behind this thing is to wrap said strings in spans with specific classes. Care to elaborate on a regular expression that would work?
Just updated now with a Regex example, working over the html.
I got there in the end :)
0

You could use a look-around assertion with regex (http://www.regular-expressions.info/lookaround.html) but that would be over complicating the problem if all you have to do is replace a single word -

$('div').find(':contains("apples")').each(function() {
    $(this).text('oranges');
});

http://jsfiddle.net/qh4w7/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.