Replace multiple strings with multiple other strings

前端 未结 18 2409
别那么骄傲
别那么骄傲 2020-11-22 04:14

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 ha

18条回答
  •  佛祖请我去吃肉
    2020-11-22 04:53

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

提交回复
热议问题