Let\'s say I have a string like "abcabcabc"
and I want the positions of \'a\'s and \'b\'s swapped so that I end up with "bacbacbac"
You can swap them (if they're always adjacent) by using a split
/join
combo:
console.log("abcabcabc".split("ab").join("ba"))
And in keeping with the split/join method, here's a way to swap them as individual characters (although it becomes significantly less readable):
console.log("aabbccaabbcc".split("a").map(s => s.split("b").join("a")).join("b"));