Categories
TypeScript Answers

How to use RegExp in TypeScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *