直接上函数吧 不废话
function replacestring(oldstr,newstr,text) { var exp = new RegExp(oldstr,'g'); var c=text.replace(exp,newstr); return c; } var oldstr='world'; var newstr='cccc'; var text='Hello world, Hello world'; var aa= replacestring(oldstr,newstr,text); console.log(aa);