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"
"abcabcabc"
"bacbacbac"
Try this :
str.replace(/[ab]/g, function($1) { return $1 === 'a' ? 'b' : 'a' })
example:
console.log("abcabcabc".replace(/[ab]/g, function($1) { return $1 === 'a' ? 'b' : 'a' }))