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
You can use https://www.npmjs.com/package/union-replacer for this purpose. It is basically a string.replace(regexp, ...)
counterpart, which allows multiple replaces to happen in one pass while preserving full power of string.replace(...)
.
Disclosure: I am the author. The library was developed to support more complex user-configurable replacements and it addresses all the problematic things like capture groups, backreferences and callback function replacements.
The solutions above are good enough for exact string replacements though.