2013-07-16 30 views
-9

如何在JavaScript中生成正則表達式來接受整數和'/'(符號),如何爲輸入值編寫正則表達式?

需要快速回復。非常感謝。

+2

你應該問一個*快速回復*嘗試過的東西。 – sp00m

+2

你已經試過了什麼? StackOverflow可以幫助你,而不是爲你寫代碼。 –

+0

以前的帖子部分解決你的問題,希望它有幫助http://stackoverflow.com/questions/9043551/regex-match-integer-only – AurA

回答

1
var a = '123/4'; 
var b = 'e123/4'; 

變1

var regex = new RegExp('^[0-9/]+$'); 

console.log((a.match(regex)) ? true : false); // true 
console.log((b.match(regex)) ? true : false); // false 

變2

var regex = /^[0-9/]+$/; 

console.log((a.match(regex)) ? true : false); // true 
console.log((b.match(regex)) ? true : false); // false 

變3

var regex = /^[\d\/]+$/; 

console.log((a.match(regex)) ? true : false); // true 
console.log((b.match(regex)) ? true : false); // false 
+0

爲什麼不'var regex =/^ [0-9 /] + $ /;' ? –

+0

我只是習慣於這樣寫。不應該是區別,對吧? – eyecatchUp

+0

順便說一句,你也可以把正則表達式寫成'/^[\ d \ /] + $ /'。 – eyecatchUp