2016-05-13 31 views

回答

1

因爲\d做同樣的事情,您不需要有[0-9]。我根據您的確切要求制定瞭解決方案,因爲您似乎有兩個不同的要求。它是8到9位數還是8到10位數,這個總數是否包括初始的0?參閱下面的這兩個解決方案:

情況1:

必須是8個或9或10位數字

嘗試:

/^0\d{7,9}$/ // or /^0\d{8,10}$/ if not including the initial 0 in the count 

情況2:

從0開始,必須有8或9或數字。

嘗試:

/^0\d{8,9}$/ // if the 8 or 9 digits does not include the initial 0 for the count 
/^0\d{7,8}$/ // if the 8 or 9 digits does include the initial 0 for the count 

詳細說明:

{8,9}指定匹配前述令牌的8或9個字符。

如果總位數包括初始0,則只需將{8,9}替換爲{7,8}(即總位數爲8或9)。如果您希望範圍爲8到10,如標題中提到的那樣,則代替{8,9},請使用{8,10}。同樣,如果不考慮初始0,這將是{7,9}

Regex101

+0

權。在那裏不需要'g'標誌。 – Tushar