jQuery / Javascript查找并替换所有实例 [英] jQuery/Javascript find and replace all instances

查看:96
本文介绍了jQuery / Javascript查找并替换所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

用Javascript / jQuery中的正则表达式替换模式的所有实例

我如何使用jQuery来设置样式/部件/特定单词的所有实例?

说我有代码:

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

现在,我要替换所有< 在div中用别的东西。

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

如果我使用 $('#div')。html()。replace('<','somethingElse'); 它只替换第一个实例。

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

如何更换所有实例?

谢谢

推荐答案

使用正则表达式

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

如果你想在div中替换它,那就试试吧。

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

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

这篇关于jQuery / Javascript查找并替换所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆