我试过这个:
Replace multiple strings at once
而这: javascript replace globally with array他们怎么没有工作.
而这: javascript replace globally with array他们怎么没有工作.
我可以这样做(它的PHP):
- $a = array('a','o','e');
- $b = array('1','2','3');
- str_replace($a,$b,'stackoverflow');
结果将是:
- st1ck2v3rfl2w
我想同时使用正则表达式.我怎样才能做到这一点 ?
谢谢.
解决方法
- var str = "I have a cat,a dog,and a goat.";
- var mapObj = {
- cat:"dog",dog:"goat",goat:"cat"
- };
- str = str.replace(/cat|dog|goat/gi,function(matched){
- return mapObj[matched];
- });