jQuery/Javascript find and replace all instances [duplicate]

Possible Duplicate:
Replace all instances of a pattern with regular expressions in Javascript / jQuery
How can I use jQuery to style /parts/ of all instances of a specific word?

Say I have the code:

<div class="replace">
     <i>Blah</i>
     <u>Blah</u>
</div>

Now, I want to replace all the < within the div with something else.

If I use $('#div').html().replace('<','somethingElse'); it only replaces the first instance.

How can I replace all the instances?

Thanks

Answers:

Answer

Use regex

"anyString".replace(/</g,'somethingElse');

If you want to replace it inside the div then try this.

$('#div').html($('#div').html().replace(/</g,'somethingElse'));

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.