[英]Find and replace multiple words JavaScript
我想用JavaScript替换一个单词,我的代码如下所示:
$('#test').contents().filter(function() {
return this.nodeType == 3
}).each(function(){
this.textContent = this.textContent.replace('microsoft', 'Hi I am
replace'),('bob', 'bobart')
});
该代码适用于第一个实例,将替换Microsoft但不会更改Bob,如何在其中添加多个要替换的单词?
谢谢
使用.replace('x','y').replace('a','b')
的替换链接:
his.textContent.replace('microsoft', 'Hi I am replace').replace('bob', 'bobart')
您可以这样做:
var replacers = {"to_be_replaced_1": "by_this_1","to_be_replaced_2":"by_this_2"},
stringToBeReplaced = "some text to_be_replaced_1";
$.each(replacers ,function(replaceable, replacer){
stringToBeReplaced.replace(replaceable, replacer);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.