There are other special characters, called quantifiers, which always follow immediately after the item they modify and dictate the number of occurrences that are needed.
Quantifier |
Descripton |
---|---|
* (Asterisk) |
Matches 0 or more of the preceding item |
+ (Plus sign) |
Matches 1 or more of the preceding item |
? (Question mark) |
Matches either zero or one of the preceding item |
Numbers in curly braces can be used for more complex quantifiers.
Example |
Description |
---|---|
{N} |
Matches exactly N occurrences |
{N, M} |
Matches at least N and at most M occurrences |
{N, } |
Matches at least N occurrences |
For example, a social security number (123-45-6789) can be found using the regular expression:
/^[0-9]{3}-[0-9]{2}-[0-9]{4}$/