jQuery/Javascript find and replace all instances [

2019-02-26 20:58发布

问题:

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

回答1:

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'));