I have a JS string that needs several of its chars replaced.
For example, for input string:
s = 'ABAC'
I would want to replace all B
s with C
s and vice versa. However, doing a standard regex replace is not good enough since the replace()
s should not occur in lockstep but rather in a single pass on the string.
>>> s.replace(/B/g, 'C').replace(/C/g, 'B')
'ABAB' // not good
Is there an elegant way to do multiple string replace()
in a single pass?
(Solution must work for any arbitrary char replacement)