Categories
TypeScript Answers

How to replace all instances of character in a string in TypeScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.