在javascript中多次只使用一个方法
我有一个脚本来替换文档的内容:在javascript中多次只使用一个方法,javascript,Javascript,我有一个脚本来替换文档的内容: var mytitle = document.title ; document.title = mytitle .replace(/old1/gi, "new1") .replace(/old2/gi, "new2") .replace(/old3/gi, "new3") .replace(/old4/gi, "new4") .replace(/old5/gi, "new5"); var mybody=document.body.innerH
var mytitle = document.title ;
document.title = mytitle
.replace(/old1/gi, "new1")
.replace(/old2/gi, "new2")
.replace(/old3/gi, "new3")
.replace(/old4/gi, "new4")
.replace(/old5/gi, "new5");
var mybody=document.body.innerHTML ;
document.body.innerHTML=mybody
.replace(/old1/gi, "new1")
.replace(/old2/gi, "new2")
.replace(/old3/gi, "new3")
.replace(/old4/gi, "new4")
.replace(/old5/gi, "new5");
您可以看到我必须编写replace(/old1/gi,“new1”).replace(/old2/gi,“new2”).replace(/old3/gi,“new3”).replace(/old4/gi,“new4”).replace(/old5/gi,“new5”)代码>2次
如何使脚本工作,甚至只是写上面的代码一次?像这样:
var myreplace=replace(/old1/gi, "new1").replace(/old2/gi, "new2").replace(/old3/gi, "new3").replace(/old4/gi, "new4").replace(/old5/gi, "new5");
var mytitle = document.title ;
document.title = mytitle.myreplace;
var mybody=document.body.innerHTML ;
document.body.innerHTML=mybody.myreplace
注意:old1
,new1
。。。是字符串。您可以将旧的
与数字的先行查找相匹配,并将其替换为新的
:
const str='foo foo old1 bar bar old2 baz baz old3';
console.log(
str.replace(/old(?=\d)/gi,“new”)
);代码>您可以将旧的
与一个数字的前向查找进行匹配,然后将其替换为新的
:
const str='foo foo old1 bar bar old2 baz baz old3';
console.log(
str.replace(/old(?=\d)/gi,“new”)
);根据你的问题,基本上你需要写一次。只需将这些步骤写入函数并调用该函数即可
var titleEditor = function(toEdit) {
return toEdit.replace(/old1/gi, "new1")
.replace(/old2/gi, "new2")
.replace(/old3/gi, "new3")
.replace(/old4/gi, "new4")
.replace(/old5/gi, "new5");
}
var mytitle = document.title ;
document.title = titleEditor(mytitle)
var mybody=document.body.innerHTML;
document.body.innerHTML= titleEditor(mybody);
现在,如果titleEditor的功能与old1、old2相同,则可以将titleEditor编写为@certainperformance
var titleEditor = function(toEdit){
return toEdit.replace(/old(\d)/gi, 'new$1')
}
另一种方法是向字符串原型添加方法
String.prototype.updateTitle = function(){
return this.replace(/old(\d)/gi, 'new$1');
}
var mytitle = document.title ;
document.title = mytitle.updateTitle()
var mybody=document.body.innerHTML;
document.body.innerHTML= mybody.updateTitle();
希望对你的问题有所帮助,基本上你需要写一次。只需将这些步骤写入函数并调用该函数即可
var titleEditor = function(toEdit) {
return toEdit.replace(/old1/gi, "new1")
.replace(/old2/gi, "new2")
.replace(/old3/gi, "new3")
.replace(/old4/gi, "new4")
.replace(/old5/gi, "new5");
}
var mytitle = document.title ;
document.title = titleEditor(mytitle)
var mybody=document.body.innerHTML;
document.body.innerHTML= titleEditor(mybody);
现在,如果titleEditor的功能与old1、old2相同,则可以将titleEditor编写为@certainperformance
var titleEditor = function(toEdit){
return toEdit.replace(/old(\d)/gi, 'new$1')
}
另一种方法是向字符串原型添加方法
String.prototype.updateTitle = function(){
return this.replace(/old(\d)/gi, 'new$1');
}
var mytitle = document.title ;
document.title = mytitle.updateTitle()
var mybody=document.body.innerHTML;
document.body.innerHTML= mybody.updateTitle();
希望对您有所帮助编写一个函数,该函数接受字符串参数并返回修改后的字符串。
函数替换内容(字符串){
如果(!string)返回;
返回字符串
.替换(/old1/gi,“new1”)
.替换(/old2/gi,“new2”)
.替换(/old3/gi,“new3”)
.替换(/old4/gi,“new4”)
.替换(/old5/gi,“new5”);
}
编写一个函数,该函数接受字符串参数并返回修改后的字符串。
函数替换内容(字符串){
如果(!string)返回;
返回字符串
.替换(/old1/gi,“new1”)
.替换(/old2/gi,“new2”)
.替换(/old3/gi,“new3”)
.替换(/old4/gi,“new4”)
.替换(/old5/gi,“new5”);
}
请发布标题的可能副本发布标题的可能副本编号“old1”、“old2”。。。是弦吗?你什么意思?/old#/gi
s是代码中的正则表达式,而不是字符串,这应该也能工作,我的意思是,它类似于replace(/abc-(.*)/gi,'abc$1')。replace(/def\nd/gi,'defd')
。@Arulraj哦,你说得对(加上数字的前瞻),谢谢大家,@Nilesh Soni帮助了我!不,“旧1”,“旧2”。。。是弦吗?你什么意思?/old#/gi
s是代码中的正则表达式,而不是字符串,这应该也能工作,我的意思是,它类似于replace(/abc-(.*)/gi,'abc$1')。replace(/def\nd/gi,'defd')
。@Arulraj哦,你说得对(加上数字的前瞻),谢谢大家,@Nilesh Soni帮助了我!谢谢你的建议!谢谢你的建议!改变内置对象和原型是非常糟糕的做法-你应该总是更喜欢使用独立函数。改变内置对象和原型是非常糟糕的做法-你应该总是更喜欢使用独立函数。