'Replace all instances of character in string in typescript?

I'm trying to replace all full stops in an email with an x character - for example "[email protected]" would become "myxemail@emailxcom". Email is set to a string.
My problem is it's not replacing just full stops, it's replacing every character, so I just get a string of x's.
I can get it working with just one full stop, so I'm assuming I'm wrong on the global instance part. Here's my code:

let re = ".";
let new = email.replace(/re/gi, "x");

I've also tried

re = /./gi;
new = email.replace(re, "x");

If anyone can shed any light I'd really appreciate it, I've been stuck on this for so long and can't seem to figure out where I'm going wrong.

** Edit: Whoops, my new variable was actually called newemail, keyword new wasn't causing the issue!



Solution 1:[1]

You can try split() and join() method that was work for me. (For normal string text) It was short and simple to implement and understand. Below is an example.

let email = "[email protected]";
email.split('.').join('x');

So, it will replace all your . with x. So, after the above example, email variable become myxemail@gmailxcom

Solution 2:[2]

You may just use replaceAll() String function, described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

If you are getting Property 'replaceAll' does not exist on type 'string' error - go to tsconfig.json and within "lib" change or add "es2021".

Like this:

enter image description here

More info here: Property 'replaceAll' does not exist on type 'string'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ajay Gupta
Solution 2 Konstantin