I need to validate some inputs using a regex.
Here are some sample use cases and expected results.
0001 - Matched
001 - Matched
01 - Matched
00.12312 - Matched
000.1232 - Matched
1 - Not matched
20 - Not matched
0.1 - Not matched
0.123123 - Not matched
What would a regex like this look like? If first char is 0
and second char is numerical[0-9]
then it is invalid.
I've tried this but it doesn't work.
[0][0-9]
Try this regex:
^0[0-9].*$
It will match anything starting with 0
followed by a digit.
It will "match" what you call "invalid".
The test code shall make it clearer:
var regExp = /^0[0-9].*$/
console.log(regExp.test("0001")); // true
console.log(regExp.test("001")); // true
console.log(regExp.test("01")); // true
console.log(regExp.test("00.12312")); // true
console.log(regExp.test("000.1232")); // true
console.log(regExp.test("1")); // false
console.log(regExp.test("20")); // false
console.log(regExp.test("0.1")); // false
console.log(regExp.test("0.123123")); // false
0+[1-9][0-9]*
Matches at least one zero, followed by a nonzero digit, followed by any number of digits. Does not match the lone 0.
You can use something like this:-
var a = "0001";
/^[0][0-9]/.test(a)
you can try this pattern, the idea is to use anchors ^
(for begin) and $
(for end) to limit the result on what you are looking for:
^0+\d*(?:\.\d+)?$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With