查找并替换多个单词JavaScript [英] Find and replace multiple words JavaScript

查看:95
本文介绍了查找并替换多个单词JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JavaScript替换一个单词,我的代码如下所示:

I am wanting to replace 1 word with another with JavaScript, the code i have is working as below:

$('#test').contents().filter(function() {
    return this.nodeType == 3
}).each(function(){
    this.textContent = this.textContent.replace('microsoft', 'Hi I am
replace'),('bob', 'bobart')    
});

该代码适用于第一个实例,它将取代Microsoft但不会更改Bob,我如何在其中添加多个要替换的单词?

The code works for the first instance and will replace Microsoft but will not change Bob, how can I add multiple words to be replaced into this?

谢谢

推荐答案

使用.replace('x','y').replace('a','b')使用替换链接:

his.textContent.replace('microsoft', 'Hi I am replace').replace('bob', 'bobart')   

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

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