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

Replace all instances of character in string in typescript?


Your second example is the closest. The first problem is your variable name, new, which happens to be one of JavaScript's reserved keywords (and is instead used to construct objects, like new RegExp or new Set). This means that your program will throw a Syntax Error.

Also, since the dot (.) is a special character inside regex grammar, you should escape it as \.. Otherwise you would end up with result == "xxxxxxxxxxxxxxxxxx", which is undesirable.

let email = "my.email@email.com"let re = /\./gi;let result = email.replace(re, "x");console.log(result)