Sometimes, we want to replace all instances of character in string in TypeScript.
In this article, we’ll look at how to replace all instances of character in string in TypeScript.
How to replace all instances of character in a string in TypeScript?
To replace all instances of character in string in TypeScript, we can use the string replace method.
For instance, we write
const email = "my.email@email.com";
const re = /\./gi;
const result = email.replace(re, "x");
console.log(result);
to define the re regex with the g flag, which matches all instances of the pattern against the string we call replace with.
Then we call email.replace to match all periods with replace them with 'x'.
Conclusion
To replace all instances of character in string in TypeScript, we can use the string replace method.