Reputation: 33
i want to replace all "E" characters in file to "Z" and all "Z" characters to "E", the problem is once i replace one of them, i cant replace the other, because then all the characters are e/z
Upvotes: 2
Views: 87
Reputation: 386560
You could use a function and an object with the replacments.
var string = 'EaEbZcZdZ';
string = string.replace(/[EZ]/g, s => ({ E: 'Z', Z: 'E' }[s]));
console.log(string);
Upvotes: 4
Reputation: 17558
Change all "E" to an unused character in the document, e.g. "$". Change "Z" to "E" and then "$" to "Z".
Upvotes: 4
Reputation: 208
Replace all E to an unused character (like ¬), then change all Z to E, and finally change all ¬ to Z.
Upvotes: 1