I can't figure out this even after searching through examples of regex. Can someone tell me what this expression means?
/.{2,}/
Thanks for your help
I can't figure out this even after searching through examples of regex. Can someone tell me what this expression means?
/.{2,}/
Thanks for your help
Greedily match two or more of any characters that are not a line separator, but....
that will depend on your engine:
.
matches any char that is not \n
... Watch out, as that includes the carriage return \r
.
matches any char that is not a line break character (\n
, \r
, paragraph separators)...{2,}
is greedy (it eats up as many characters as possible)
Note that the {2,}
quantifier is "greedy": .{2,}
will match as many characters as possible, and will only "give back characters" (backtrack) if necessary, for instance to allow the next token (if any) to match.
DOTALL
mode
In dotall
mode (which you turn on differently in various engine), the .
will also match line separators.
s
flagm
flag.
matches any character except the newline character. Quoting from MDN's RegExp page,
(The decimal point) matches any single character except the newline character.
{2,}
means two or more times.
So, the entire expression means that, "match any set of characters with length greater than or equal to 2"
{ , }
is a repetition quantifier. it is be used to specify how many times any token can be repeated against a successful match.
quoting from Limiting Repetition
: (HERE)
The syntax is {min,max}
, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. If the comma is present but max is omitted, the maximum number of matches is infinite.
Thus, {2,}
would mean 2 or more of the preceeding character.
the .
represents any character (other than line separator
) and the forward-slashes
indicate the start and end of the regular expression.
UPDATE:
So far I have this regex for characters allowed. /^[a-zA-Z0-9'.,\s\-&()]*$/ Do I need to keep /.{2,}/ in the field?
{a,b}
specifies minimum (through a) and maximum (through b) limits for a matching string. therefore, if you need to specify a min and/or max limit for your letters, numbers, commas,and periods
you may enclose the regex for these characters in a group ( such as [ ]
) and place { a, b}
right after this group, like this: /^[a-z]{2,200}$/
. This will match a string of 2 to 200 lowercase alphabets.
as for your letters, numbers, commas,and periods
, in the regex /^[a-zA-Z0-9'.,\s\-&()]*$/
you'll need to escape .
and replace *
with {2,max-limit}
, as following:
/^[a-zA-Z0-9'\.,\s\-&()]{2,20}$/
--> DEMO