Replace all instances of a character in a string in typescript?

I am trying to replace all complete stops in the letter with the x symbol - for example, " my.email@email.com " will become "myxemail @emailxcom". Email is set to string.
My problem is that it does not replace only complete stops, it replaces every character, so I just get the string x.
I can get it to work with one full stop, so I assume that I am mistaken in the global part of the instance. Here is my code:

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

I also tried

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

If someone can shed light, I would be very grateful for this, I have lingered on this for so long and cannot understand where I am mistaken.

** Edit: Oops, my new variable is actually called newemail, the new keyword did not cause a problem!

+7
source share
2 answers

Your second example is the closest. The first problem is the name of your variable , which is one of the reserved JavaScript keywords (and is used instead to create objects like or ). This means that your program will throw a syntax error. newnew RegExpnew Set

, (.) - , \. , result == "xxxxxxxxxxxxxxxxxx", .

let email = "my.email@email.com"

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

console.log(result)
Hide result
+21

split() join() . ( ) . .

let email = "my.email@email.com";
email.split('.').join('x');

, . x. , , email myxemail@gmailxcom

0

Source: https://habr.com/ru/post/1674432/


All Articles