首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

js replaceAll步骤实现

2012-09-04 
js replaceAll方法实现js 代码String.prototype.replaceAll function(s1,s2){return this.replace(new Re

js replaceAll方法实现
js 代码

String.prototype.replaceAll  = function(s1,s2){    return this.replace(new RegExp(s1,"gm"),s2);    }  



方法: string.replace(new RegExp(oldString,"gm"),newString))

gm     g=global, m=multiLine  ,  大致上方法就是这样的,可以实现替换全部指定字串

另一个简单的验证JS的方法:

在浏览器地址栏输入
javascript:alert("abcabcabc".replace(new RegExp("a","gm"),"ad"))

这样比较省事 ;)     ,不知道多行的会不会很方便



orgStr.replace(new RegExp(findStr, 'g'), replaceStr)
应该就可以替换所有的了
如果不用正则表达式
orgStr.replace(findStr, replaceStr)只能替换第一个

热点排行