2013-11-22 33 views

回答

1

我認爲這是一件事,但顯然不是。爲什麼一個人會這麼想?

如果你看起來真的很快,它確實欺騙了你。但運營商的實施並不完整,將會失敗。

以下範圍的量詞/運營商的認可:

{n}  Match the preceding exactly n times 
{n,} Match the preceding at least n times 
{n,m} Match the preceding at least n but not more than m times 
{n,}? Match the preceding at least n times, but as few times as possible. 
{n,m}? Match the preceding between n and m times, but as few times as possible. 

中與使用操作必須設置{n齊全,應有盡有以下,m}是可選的。

正確使用量詞/運算符的示例。

"12345".match(/\d{3}/); // => matches '123' 
"12345".match(/\d{5,}/); // => matches '12345', FAILS on 1234 
"12345".match(/\d{1,4}/); // => matches '1234' 
"12345".match(/\d{2,}?/); // => matches '12' 
"12345".match(/\d{2,4}?/); // => matches '12' 
2

量詞{n1,n2}一個有效的JavaScript正則表達式量詞,它將匹配n1到n2次(含)。

但是,{,n}確實不是代表量化因爲需要最小限制。有關語法制作和規則,請參見部分15.10.2.7 Quantifier

以下所有介紹的有效範圍量詞:

/\d{3,5}/.test('12')  // false 
/\d{3,5}/.test('1234') // true 
/\d{3,5}/.test('123456') // false 

在另一方面,下面的正則表達式創建一個量詞。相反,生產被解析成一個文本,沒有特別的含義:

/a{,5}b/.test('a{,5}b') // true, at least in Chrome and IE 
+0

我明白,maxbound是可選的,這正是我所想的。 – user2958725