使用OR运算符(
|):
var str = '#this #is__ __#a test###__';str.replace(/#|_/g,''); // result: "this is a test"
您还可以使用字符类:
str.replace(/[#_]/g,'');
Fiddle
如果您想用一件事替换哈希值,而用另一件事替换下划线,则只需要链接即可。但是,您可以添加一个原型:
String.prototype.allReplace = function(obj) { var retStr = this; for (var x in obj) { retStr = retStr.replace(new RegExp(x, 'g'), obj[x]); } return retStr;};console.log('aabbaabbcc'.allReplace({'a': 'h', 'b': 'o'}));// console.log 'hhoohhoocc';但是为什么不连锁呢?我认为这没有错。



