2014-02-11 56 views
-4
var numbonly= /^[0-9]{3}\d+$/; 

請你能告訴我這個正則表達式在JavaScript中意味着什麼嗎?我對這件事很陌生並且非常堅持。

+3

奇數。爲什麼有人會在同一個正則表達式中使用'[0-9]'和'\ d'? – user2357112

+1

http://www.regular-expressions.info/ – Trent

+0

@ user2357112我正要指出相同的 – exexzian

回答

0
^  Matches the beginning of the String 
[0-9] Matches characters in the range 0-9 
{3} Matches the previous token [0-9] exactly 3 times 
\d Matches any digit character 
+  Matches previous token \d one or more times 
$  Matches the end of the String 
2

^ =字符串必須

[0-9] =任何數字0-9

{3} =開始必須有3位數字

\d =任何數字(以下簡稱[0-9 ])

+ = +是{1,}的縮寫。匹配一個或多個次

$ =串

的端

所以在英國,必須有一個數字[0-9],3倍,然後另一個數字[0-9]必須存在1個或多個倍。所以基本上這意味着4位或更多的數字。所以它可以寫得更短,就像這樣....

^\d{4,}$ 
+0

謝謝非常多 – user3295255

+0

@ user3295255我看到你是新的stackoverflow。如果其中一個答案成功解答了您的問題,請點擊旁邊的複選標記以接受它。謝謝。 – gfrobenius