javascript - Regex expression validation for numbers -
this regex expression have validate situation below:
correct input:
- 12345678,12345678,12345678
- *space*12345678 , 12345678 , 12345678 , 12345678
- 12345678,12345678,*space*
- 12345678
- *space*12345678,
- 12345678,
result: return true(regex expression working correctly situations above.)
wrong input:
- 1234567812345678
result: return true (should false)
for wrong input should returning false , return true. should validate wrong input?
var validate_commas = /^(\s*\d{8}\s*[,]?\s*)*$/;
thank you
see ambiguity in description more details on why question ambiguous, , how affects answers get.
i assume spaces (ascii 32) arbitrarily allowed:
- at start of string
- at end of string
- before , after comma.
i assume want disallow horizontal tabs, new lines, carriage returns , other whitespace characters matched \s
, , allow space (ascii 32) freely specified.
the bnf grammar above assumptions is:
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" <number> ::= <digit> {8} <space> ::= " " <token> ::= <space>* <number> <space>* <list> ::= <token> ( "," <token> )* [ "," <space>* ]
with grammar above, easy write regex solution:
var regex = /^ *\d{8} *(?:, *\d{8} *)*(?:, *)?$/;
ambiguity in description
rather showing examples of string want match, suggested, should specify grammar input. examples may not cover cases, , answerer assumes "don't care" or make assumptions cases not stated in examples.
Comments
Post a Comment