2015-07-28 89 views
0

我想寫的函數,它確切地告訴我,表達式適合正則表達式。 例如。Js正則表達式確切模式

<script> 
var pattern = /[0-9]{2}/ 

alert(pattern.test("1236")); 

</script> 

這個表達式爲真,但我想假的,因爲我想只有兩個數字

如。

alert(pattern.test("25")); 

只有這應該是真的。

如何更改我的代碼?

+0

http://www.regular-expressions.info/anchors.html –

回答

1

用途:

var pattern = /^[0-9]{2}$/ 

現在說:

Two digits from start "^" to the end "$"