扫码关注官方订阅号
a = "testtesttest http://baidu.com,http://baidu.com,test" rgx=/()?((((http|ftp|https)://)|(www.))+[w-_]+(.[w-_]+)+([w-.,@?^=%&:/~+#][w-@?^=%&/~+#])?)('?>?.*)?([/]|[W])/gi;
a.replace(rgx, "~hh~")
想要的效果是是"testtesttest ~hh~,~hh~,test"实际上只替换掉了第一个,如何实现全部替换呢?
拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...
String.prototype.replaceAll = function (reallyDo, replaceWith, ignoreCase) {
if (!RegExp.prototype.isPrototypeOf(reallyDo)) { return this.replace(new RegExp(reallyDo , (ignoreCase ? "gi" : "g")), replaceWith); } else { return this.replace(reallyDo, replaceWith); }
};
// eg:var a = "aaabbbbaaabbcccbdddd";// 把所有的"a"替换为"v"a = a.replaceAll("a","v");
'哈哈哈-sss-fdf-蛤蛤蛤'.replace(/-/g,"")
String.prototype.replaceAll = function(s1, s2){ return this.replace(new RegExp(s1, 'gm'),s2); };
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
String.prototype.replaceAll = function (reallyDo, replaceWith, ignoreCase) {
};
// eg:
var a = "aaabbbbaaabbcccbdddd";
// 把所有的"a"替换为"v"
a = a.replaceAll("a","v");
'哈哈哈-sss-fdf-蛤蛤蛤'.replace(/-/g,"")