2016-07-27 75 views
0

我一直在尋找一個JavaScript正則表達式模式,將允許在插入或註冊這些類型的量不爲金錢價值的JavaScript正則表達式模式沒有貨幣

5 
5.1 
5.10 
5 
500,000 
500,000.1 
500,000.10 

,但這些數字不應該比0。我設法較小找到模式,但我想省略「$」。我要的是一個數字,逗號和點只有

var x = 5000; 
var a = "5,000,000" 
var y = "$5,,,000,000" 
var z = "500,000" 
var c = "c231" 
console.log(x," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(x)) 
console.log(a," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(a)) 
console.log(y," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(y)) 
console.log(z," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(z)) 
console.log(c," Valid", /^\$?\d+(,\d{3})*(\.\d*)?$/.test(c)) 
+1

刪除\ $?來自RegEx –

+1

新RegEx將/^\d+(,\d{3})*(\.\d*)?$/ –

+0

我的回答有幫助嗎?考慮接受它,如果它。 – fuzz

回答

3

取出\$?

/^\d+(,\d{3})*(\.\d*)?$/

var x = 5000; 
 
var a = "5,000,000" 
 
var y = "$5,,,000,000" 
 
var z = "500,000" 
 
var c = "c231" 
 
var d = "1...0.00" 
 
var e = "..." 
 
var f = "1.00,,," 
 

 
var validRegEx = /^\d+(,\d{3})*(\.\d*)?$/; 
 

 
console.log(x," Valid", validRegEx.test(x)) 
 
console.log(a," Valid", validRegEx.test(a)) 
 
console.log(y," Valid", validRegEx.test(y)) 
 
console.log(z," Valid", validRegEx.test(z)) 
 
console.log(c," Valid", validRegEx.test(c)) 
 
console.log(d," Valid", validRegEx.test(c)) 
 
console.log(e," Valid", validRegEx.test(c)) 
 
console.log(f," Valid", validRegEx.test(c))

0

基於我對問題評論的新代碼將是

var x = 5000; 
var a = "5,000,000" 
var y = "$5,,,000,000" 
var z = "500,000" 
var c = "c231" 
console.log(x," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(x)) 
console.log(a," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(a)) 
console.log(y," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(y)) 
console.log(z," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(z)) 
console.log(c," Valid", /^\d+(,\d{3})*(\.\d*)?$/.test(c)) 
0

嘗試

^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(\.[0-9][0-9])?$