Shlomo
Shlomo

Reputation: 3990

Regex number starting with 0 or allow nothing at all

Trying to create a regex that allows typing in the following input into a html input:

^[^1-9]{0,1}[0-9\\s-\\/]*$

With the following regex I cannot delete the 0...

^0[0-9\\s-\\/]*$"

Upvotes: 1

Views: 96

Answers (2)

vks
vks

Reputation: 67988

^(?:0[0-9 \\/-]*|)$

You can use this instead.See demo.This will allow empty as well as strings starting from 0.

https://regex101.com/r/uF4oY4/18

Upvotes: 0

Siguza
Siguza

Reputation: 23870

Add a non-capturing group (?:...) around everything between ^ and $, and use the ? operator on it (allow zero or one):

^(?:0[0-9\\s-\\/]*)?$

Upvotes: 3

Related Questions