Categories
JavaScript Answers

How to define regular expression for number with length of 4, 5 or 6 with JavaScript?

Spread the love

Sometimes, we want to define regular expression for number with length of 4, 5 or 6 with JavaScript.

In this article, we’ll look at how to define regular expression for number with length of 4, 5 or 6 with JavaScript.

How to define regular expression for number with length of 4, 5 or 6 with JavaScript?

To define regular expression for number with length of 4, 5 or 6 with JavaScript, we use the \d pattern.

For instance, we write

const re = /\d{4,6}/;

to define the re regex that matches strings with 4 to 6 digits.

\d matches digits.

And {4,6} matches digit groups with length between 4 and 6.

Conclusion

To define regular expression for number with length of 4, 5 or 6 with JavaScript, we use the \d pattern.

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 *