Idan Horowitz
Idan Horowitz

Reputation: 33

Replacing all "E" characters in file to "Z" and all "Z" characters to "E"

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

Answers (3)

Nina Scholz
Nina Scholz

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

JSON Derulo
JSON Derulo

Reputation: 17558

Change all "E" to an unused character in the document, e.g. "$". Change "Z" to "E" and then "$" to "Z".

Upvotes: 4

Rafael del Rio
Rafael del Rio

Reputation: 208

Replace all E to an unused character (like ¬), then change all Z to E, and finally change all ¬ to Z.

Upvotes: 1

Related Questions