Currently i am using this regex to match for positive numbers with a single decimal point
目前我正在使用此正则表达式来匹配具有单个小数点的正数
/^\d+(\.\d+)?$/
But this doesn't allow commas. How can i modify this to allow zero or more commas before the decimal point?
但这不允许使用逗号。如何修改它以允许小数点前的零或更多逗号?
Example :
- 11,111.00 (should be allowed) I am okay with numbers having any number of comma's before decimal point.
11,111.00(应该被允许)我可以使用小数点前有任意数字逗号的数字。
EDIT:
Valid values
- 111
- 11,111
- 11,111.0
- 111111
The values can be entered with or without comma. The datatype of this field is SQL MONEY, so it will handle comma's.
可以使用或不使用逗号输入值。该字段的数据类型是SQL MONEY,因此它将处理逗号。
4 个解决方案
#1
3
need
/^(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d+)?$/
See the regex demo
请参阅正则表达式演示
Details
-
^
- start of string -
(?:\d{1,3}(?:,\d{3})*|\d+)
- Either of:-
\d{1,3}(?:,\d{3})*
- 1 to 3 digits followed with 0+ sequences of a,
and 3 digits -
|
- or -
\d+
- 1 or more digits
\ d {1,3}(?:,\ d {3})* - 1到3位数后面跟着0和3位数的序列
| - 要么
\ d + - 1位或更多位数
-
-
(?:\.\d+)?
- an optional sequence of.
and 1+ digits -
$
- end of string.
^ - 字符串的开头
(?:\ d {1,3}(?:,\ d {3})* | \ d +) - 下列之一:\ d {1,3}(?:,\ d {3})* - 1到3位数后跟0和3位数的序列| - 或\ d + - 1位或更多位数
(?:\。\ d +)? - 一个可选的序列。和1+位数
$ - 结束字符串。
var strs = [',,,,', '111', '11,111', '11,111.0', '111111'];
var re = /^(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d+)?$/;
for (var s of strs) {
console.log(s + " => " + re.test(s));
}
#2
1
This is a very simple general solution, without any assumptions about how many digits are needed.
这是一个非常简单的通用解决方案,没有任何关于需要多少位数的假设。
/^\d[\d,]*(\.\d+)?$/
[\d,]
will match digits or commas.
You could make the regex more complicated if you really need it to be more specific.
[\ d,]将匹配数字或逗号。如果你真的需要它更具体,你可以使正则表达式更复杂。
#3
1
I would use this
我会用这个
^(?:\d{1,3}(,\d{3})*|\d+|\d{2}(?:,\d{2})*,\d{3})(?:\.\d+)?$
请参阅演示和说明
#4
0
This is pretty hard to read but I'll explain it
这很难读,但我会解释一下
/^(?:\d+)(?:(?:\d+)|(?:(?:,\d+)?))+(?:\.\d+)?$/
All the ?:
are just to explicitly say to the regex engine "Don't capture the following group matched by this paren).
所有?:只是明确地对正则表达式引擎说“不要捕获这个paren匹配的下一组”。
The simplified version would be
简化版将是
/^(\d+)((\d+)|((,\d+)?))+(\.\d+)?$/
But it'd capture a lot of matching groups for no reason so I removed them
但它无缘无故地捕获了很多匹配组,所以我删除了它们
#1
3
need
/^(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d+)?$/
See the regex demo
请参阅正则表达式演示
Details
-
^
- start of string -
(?:\d{1,3}(?:,\d{3})*|\d+)
- Either of:-
\d{1,3}(?:,\d{3})*
- 1 to 3 digits followed with 0+ sequences of a,
and 3 digits -
|
- or -
\d+
- 1 or more digits
\ d {1,3}(?:,\ d {3})* - 1到3位数后面跟着0和3位数的序列
| - 要么
\ d + - 1位或更多位数
-
-
(?:\.\d+)?
- an optional sequence of.
and 1+ digits -
$
- end of string.
^ - 字符串的开头
(?:\ d {1,3}(?:,\ d {3})* | \ d +) - 下列之一:\ d {1,3}(?:,\ d {3})* - 1到3位数后跟0和3位数的序列| - 或\ d + - 1位或更多位数
(?:\。\ d +)? - 一个可选的序列。和1+位数
$ - 结束字符串。
var strs = [',,,,', '111', '11,111', '11,111.0', '111111'];
var re = /^(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d+)?$/;
for (var s of strs) {
console.log(s + " => " + re.test(s));
}
#2
1
This is a very simple general solution, without any assumptions about how many digits are needed.
这是一个非常简单的通用解决方案,没有任何关于需要多少位数的假设。
/^\d[\d,]*(\.\d+)?$/
[\d,]
will match digits or commas.
You could make the regex more complicated if you really need it to be more specific.
[\ d,]将匹配数字或逗号。如果你真的需要它更具体,你可以使正则表达式更复杂。
#3
1
I would use this
我会用这个
^(?:\d{1,3}(,\d{3})*|\d+|\d{2}(?:,\d{2})*,\d{3})(?:\.\d+)?$
请参阅演示和说明
#4
0
This is pretty hard to read but I'll explain it
这很难读,但我会解释一下
/^(?:\d+)(?:(?:\d+)|(?:(?:,\d+)?))+(?:\.\d+)?$/
All the ?:
are just to explicitly say to the regex engine "Don't capture the following group matched by this paren).
所有?:只是明确地对正则表达式引擎说“不要捕获这个paren匹配的下一组”。
The simplified version would be
简化版将是
/^(\d+)((\d+)|((,\d+)?))+(\.\d+)?$/
But it'd capture a lot of matching groups for no reason so I removed them
但它无缘无故地捕获了很多匹配组,所以我删除了它们