jQuery/Javascript find and replace all instances
12,399
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'));
Author by
user1083320
Updated on June 14, 2022Comments
-
user1083320 almost 2 years
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
-
Phiber over 9 yearsHow I do if I want to do this for whole html? $('#html').html($('#html').replace(/name/g, 'somethingElse')); does'nt work.
-
Mr_Spock about 9 years@Phiber Did you ever learn how? If so, let me know, please.