Replace multiple strings with multiple other strings

前端 未结 18 2285
别那么骄傲
别那么骄傲 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 05:07

    With my replace-once package, you could do the following:

    const replaceOnce = require('replace-once')
    
    var str = 'I have a cat, a dog, and a goat.'
    var find = ['cat', 'dog', 'goat']
    var replace = ['dog', 'goat', 'cat']
    replaceOnce(str, find, replace, 'gi')
    //=> 'I have a dog, a goat, and a cat.'
    

提交回复
热议问题