I am trying to swap all occurrences of a pair of substrings within a given string.
For example, I may want to replace all occurrences of \"coffee\" with \"tea\" and
Based on @alexander-gessler's answer, but with support for dynamic inputs.
(While potentially opening the door to code injection.)
function swapSubstrings(string, a, b) { return string.replace( new RegExp(`(${a}|${b})`, "g"), match => match === a ? b : a ) }