3

the following variable …

var breadcrumbs = $('#trail').html();

… contains:

<href="page1/">Page 1</a> <strong>»</strong> <a href="page2">Page 2</a> <strong>»</strong> <a href="page3">Page 3</a>

How can I replace each <strong>»</strong> with something else?

2
  • Generally it would be better to do such manipulations as DOM elements instead of as a String. Could you describe what you're ultimately trying to do? Commented Mar 7, 2011 at 23:01
  • Well whatever. Just be wary of subtle changes to the HTML string returned by .html() in some browsers that will break the .replace(). Commented Mar 7, 2011 at 23:10

2 Answers 2

15

Just a regular old replace should do it. Be careful about », it might be written as &raquo; in the markup or something...

breadcrumbs.replace( new RegExp("<strong>»</strong>", 'g'), "something else" );
Sign up to request clarification or add additional context in comments.

1 Comment

Sidenote: » really should be written as &raquo; in your markup.
5
breadcrumbs.replace("<strong>»</strong>","new string");

or, depending on how you have written the » character

breadcrumbs.replace("<strong>&raquo;</strong>","new string");

3 Comments

The difference between this and my answer is that this will only replace a single instance in the breadcrumbs string.
You hadn't answered when I posted my answer. We must have posted at the same time.
Yea, it happens. Just wanted to point out the differences in our code for the asker.

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.