regular expression 1: check numbers between 3 , 6.
var myregxp = /^([0-9]){3,6}$/;
regular expression 2: check numbers between 3 , 6.
var myregxp = /^([0-9_]){3,6}$/;
regular expression3: check numbers between 3 , 6 ,
dash
.var myregxp = /^([0-9_-]){3,6}$/;
questions:
- why these work numbers matching between 3 , 6?
- what meaning of
_
(underscore) in second , third expression?
each of these checks 3 6 characters sequence.
the first allows 3 digits only.
the second allows 3 characters, including underscores in addition digits.
the third allows 3 characters, including underscores , dashes in addition digits.
whether these "valid" or not depends upon you're trying accomplish. dash useful allow negative numbers, though more rigorous regex require dash in first position , still allow 3 6 additional digits.
in answer question underscore, allowable character in third regex. it's legal regex.
if dissect third regular expression, this:
^
means start of string matching
()
means capture between parens separately in results. doesn't affect matches, affects how matches results returned.
[]
denotes character set can match in brackets.
[0-9]
denotes range of characters character between 0 , 9 constitute match.
[0-9_-]
denotes same range above, includes underscore character , hyphen character.
{3,6}
means want match 3-6 occurrences of previous regex element.
$
means end of string
so, in third regex, you're looking beginning of string, followed 3-6 characters can numeric digit, underscore or hyphen followed end of string.
by way of example:
"444" - matches 3 "-44" - matches second or third "_-4" - matches third "4" - matches none
if want regex allowed 3-6 digits , positive or negative, use this:
/^[+\-]?\d{3,6}$/
this allows optional leading hyphen or plus, followed 3-6 digits.
Comments
Post a Comment