Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

javascript - 用多个其他字符串替换多个字符串(Replace multiple strings with multiple other strings)

I'm trying to replace multiple words in a string with multiple other words.

(我正在尝试用多个其他单词替换字符串中的多个单词。)

The string is "I have a cat, a dog, and a goat."

(字符串是“我有一只猫,一只狗和一只山羊”。)

However, this does not produce "I have a dog, a goat, and a cat", but instead it produces "I have a cat, a cat, and a cat".

(但是,这不会产生“我有一只狗,一只山羊和一只猫”,而是产生“我有一只猫,一只猫和一只猫”。)

Is it possible to replace multiple strings with multiple other strings at the same time in JavaScript, so that the correct result will be produced?

(在JavaScript中是否可以同时用多个其他字符串替换多个字符串,以便产生正确的结果?)

var str = "I have a cat, a dog, and a goat.";
str = str.replace(/cat/gi, "dog");
str = str.replace(/dog/gi, "goat");
str = str.replace(/goat/gi, "cat");

//this produces "I have a cat, a cat, and a cat"
//but I wanted to produce the string "I have a dog, a goat, and a cat".
  ask by Anderson Green translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Specific Solution(具体解决方案)

You can use a function to replace each one.

(您可以使用一个函数替换每个函数。)

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];
});

jsfiddle example

(jsfiddle示例)

Generalizing it(概括它)

If you want to dynamically maintain the regex and just add future exchanges to the map, you can do this

(如果您想动态维护正则表达式并仅将将来的交换添加到地图,则可以执行此操作)

new RegExp(Object.keys(mapObj).join("|"),"gi"); 

to generate the regex.

(生成正则表达式。)

So then it would look like this

(所以它看起来像这样)

var mapObj = {cat:"dog",dog:"goat",goat:"cat"};

var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
str = str.replace(re, function(matched){
  return mapObj[matched];
});

And to add or change any more replacements you could just edit the map.

(要添加或更改更多替换,您只需编辑地图即可。)

fiddle with dynamic regex

(摆弄动态正则表达式)

Making it Reusable(使其可重用)

If you want this to be a general pattern you could pull this out to a function like this

(如果您希望将其作为常规模式,则可以将其拉出类似这样的函数)

function replaceAll(str,mapObj){
    var re = new RegExp(Object.keys(mapObj).join("|"),"gi");

    return str.replace(re, function(matched){
        return mapObj[matched.toLowerCase()];
    });
}

So then you could just pass the str and a map of the replacements you want to the function and it would return the transformed string.

(因此,您只需将str和所需替换的映射传递给函数,它将返回转换后的字符串。)

fiddle with function

(摆弄功能)

To ensure Object.keys works in older browsers, add a polyfill eg from MDN or Es5 .

(为了确保Object.keys在较旧的浏览器中可以正常工作,请添加一个polyfill例如从MDNEs5 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...