I need to make sure that string matches rule that data matches following pattern
^\d{1,4}\,?
Basically it can be a list of numbers between 1-4 characters followed by ",". So this would be valid
1,12,123,1234 but 12345,123, would not. How does one tell it to repeat match? I tried grouping it and adding + at the end but that does not work.
请尝试以下操作:
/^(?:\d{1,4}(,|$))+$/
Regex should be using $
also to make sure entire input is matched and use quantifier +
for repetition:
var re = /^(\d{1,4}(,|$))+$/;
And use RegExp.test
method to validate the input:
var valid = re.test(str);
Testing:
re.test('1,12,123,1234');
true
re.test('12345,123');
false
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.