Quantified Character
Learn about the quantified character in RegEx.
Overview of quantified character
Let’s consider the problem we were working on in the previous lesson. You can re-write the RegEx as /\(\d+\)-\(\d+\)-\(\d+\)/
. This way, the RegEx was shortened and much easier to understand. In this RegEx, it will have at least one occurrence of the number character set. However, this would match the five-digit number as well. How do we mention it a specific number of times? RegEx utilizes the quantified character to overcome this problem.
Usage of quantified character
If you want the specific number of times, then there is a range {min, max}, which is written using curly braces that provides the minimum and maximum times allowed. the minimum number is required whereas the maximum is optional.
Ways to write quantified repetition
/{min,max}/
at-least and at-max occurrence of preceding character./{min}/
minimum and maximum are the same, which means the exact number of times of preceding character./{min,}/
at least occurrence of preceding character.
Negative numbers are not allowed inside "{}"
curly braces.
Now, the problem is solved. You can use quantified repetition characters to avoid text like “goood” and any five-digit combination.
Your RegEx will become /\(\d{4}\)-\(\d{3}\)-\(\d{3}\)/
. The RegEx engine will match parentheses and any four-digit combination of numbers, neither less than four nor greater than four. Exactly the four-digits. In a another group, the same thing would occur.
Get hands-on with 1400+ tech skills courses.