我需要将“_”的每个实例替换为空格,并将“#”的每个实例替换为空/空。
var string = '#Please send_an_information_pack_to_the_following_address:';
我试过这个:
string.replace('#','').replace('_', ' ');
我真的不喜欢这样的链接命令。还有另一种方法可以做到这一点吗?
使用或运算符 (|
):
var str = '#this #is__ __#a test###__';
console.log(
str.replace(/#|_/g, '') // "this is a test"
)
您还可以使用字符类:
str.replace(/[#_]/g,'');
Fiddle https://jsfiddle.net/fnRcL/
如果你想用一个东西替换散列,用另一个东西替换下划线,那么你只需要链接
function allReplace(str, obj) {
for (const x in obj) {
str = str.replace(new RegExp(x, 'g'), obj[x]);
}
return str;
};
console.log(
allReplace( 'abcd-abcd', { 'a': 'h', 'b': 'o' } ) // 'hocd-hocd'
);
但为什么不连锁呢?我认为这没有什么问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)