解释这个正则表达式请var numbonly= /^[0-9]{3}d+$/;


var numbonly= /^[0-9]{3}d+$/;

你能告诉我这个正则表达式在 JavaScript 中是什么意思吗?我对这件事很陌生,很卡住。

^ = 字符串必须以

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

{3} = 必须有 3 位数字

d = 任何数字([0-9] 的缩写)

+ = + 是 {1,} 的缩写。匹配一次或多次

$ = 字符串结尾

所以在英语中,必须有一个数字 [0-9],3 次,然后另一个数字 [0-9] 必须出现 1 次或更多次。 所以基本上这意味着 4 位或更多位数字。 所以它可以写得更短,就像这样....

^d{4,}$
^     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

最新更新