Sometimes, we want to use RegExp in TypeScript.
In this article, we’ll look at how to use RegExp in TypeScript.
How to use RegExp in TypeScript?
To use RegExp in TypeScript, we can use it as we do with JavaScript.
For instance, we write
const trigger = "2";
const regexp = new RegExp("^[1-9]d{0,2}$");
const test = regexp.test(trigger);
to create a new regex with the RegExp
constructor that matches any number with 0 to 2 digits.
Then we call test
with the trigger
string to see if it matches.
Conclusion
To use RegExp in TypeScript, we can use it as we do with JavaScript.