Javascript regex允许一个字符(不应该)为什么?
您好,我正在尝试创建一个正则表达式,可以识别输入的货币和数字。我必须允许数字,因为我希望以编程方式输入未格式化的数字,然后我将自己格式化它们。出于某种原因,我的正则表达式允许一个字母字符作为可能的输入Javascript regex允许一个字符(不应该)为什么?,javascript,jquery,regex,jquery-inputmask,Javascript,Jquery,Regex,Jquery Inputmask,您好,我正在尝试创建一个正则表达式,可以识别输入的货币和数字。我必须允许数字,因为我希望以编程方式输入未格式化的数字,然后我将自己格式化它们。出于某种原因,我的正则表达式允许一个字母字符作为可能的输入 [\$]?[0-9,]*\.[0-9][0-9] 我知道我的正则表达式接受添加多个逗号的情况,并且小数点后还需要两位数字。我已经有了解决这个问题的办法。我已经把问题缩小到了*\。 编辑 我找到了工作正常的正则表达式[\$]?([0-9,])*[\.][0-9]{2},但我仍然不知道它是如何或为什
[\$]?[0-9,]*\.[0-9][0-9]
我知道我的正则表达式接受添加多个逗号的情况,并且小数点后还需要两位数字。我已经有了解决这个问题的办法。我已经把问题缩小到了*\。
编辑
我找到了工作正常的正则表达式[\$]?([0-9,])*[\.][0-9]{2}
,但我仍然不知道它是如何或为什么失败的
我正在使用.formatCurrency()
将输入格式化为货币格式。可以找到它,但它仍然允许我使用alpha字符,因此我必须使用$(this.inputmask('Regex',{Regex:[\$]?([0-9,])*[\.][0-9]{2}})进一步屏蔽它
其中找到输入掩码,$(this)
是对文本类型的输入元素的引用。我的代码看起来像这样
<input type="text" id="123" data-Money="true">
//in the script
.find("input").each(function () {
if ($(this).attr("data-Money") == "true") {
$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
$(this).on("blur", function () {
$(this).formatCurrency();
});
//在剧本中
.find(“input”).each(函数(){
如果($(this.attr(“数据货币”)==“true”){
$(this.inputmask('Regex',{Regex:[\$]?([0-9,])*[\.][0-9]{2}});
$(此).on(“模糊”,函数(){
$(this.formatCurrency();
});
我希望这会有所帮助。我尝试创建一个JSFIDLE,但Idk如何添加外部库/插件/扩展首先,您可以将“[0-9]”替换为“\d”。因此,我们可以将您的第一个正则表达式重写得更干净一些
\$?[\d,]*\.\d\d
分解如下:
\$? - A literal dollar sign, zero or one
[\d,]* - Either a digit or a comma, zero or more
\. - A literal dot, required
\d - A digit, required
\d - A digit, required
从这里,我们可以看到最小的合法字符串是\。\d\d
,三个字符长。您提供的正则表达式永远不会针对任何一个字符串进行验证
看看你的第二个正则表达式
[\$]? - A literal dollar sign, zero or one
([0-9,])* - Either a digit or a comma, subexpression for later use, zero or more
[\.] - A literal dot, required
[0-9]{2} - A digit, twice required
这与上面的最小可匹配字符串完全相同-\。\d\d
编辑:如前所述,根据您可能需要转义前斜杠的语言,以确保它们在处理字符串时不会被语言误解
另外,作为旁白,下面的正则表达式可能更接近您所需要的
[A-Z]{3} ?(\d{0,3}(?:([,. ])\d{3}(?:\2\d{3})*)?)(?!\2)[,.](\d\d)\b
说明:
[A-Z]{3} - Three letters; for an ISO currency code
? - A space, zero or more; for readability
( - Capture block; to catch the integer currency amount
\d{0,3} - A digit, between one and three; for the first digit block
(?: - Non capturing block (NC)
([,. ]) - A comma, dot or space; as a thousands delimiter
\d{3} - A digit, three; the first possible whole thousands
(?: - Non capturing block (NC)
\2 - Match 2; the captured thousands delimiter above
\d{3} - A digits, three
)* - The above group, zero or more, i.e. as many thousands as we want
)? - The above (NC) group, zero or one, ie. all whole thousands
) - The above group, i.e everything before the decimal
[.,] - A comma or dot, as a decimal delimiter
(\d{2}) - Capture, A digit, two; ie. the decimal portion
\b - A word boundry; to ensure that we don't catch another
digit in the wrong place.
年约翰·库格曼(John Kugelman)的回答提供了负面展望
这正确匹配(匹配项括在方括号内):
但不是:
GBP 1.000
YEN 200,000
首先,您可以将“[0-9]”替换为“\d”。因此,我们可以将您的第一个正则表达式重写得更干净一些,如下所示:
\$?[\d,]*\.\d\d
分解如下:
\$? - A literal dollar sign, zero or one
[\d,]* - Either a digit or a comma, zero or more
\. - A literal dot, required
\d - A digit, required
\d - A digit, required
从这里,我们可以看到最小的合法字符串是\。\d\d
,三个字符长。您提供的正则表达式永远不会针对任何一个字符串进行验证
看看你的第二个正则表达式
[\$]? - A literal dollar sign, zero or one
([0-9,])* - Either a digit or a comma, subexpression for later use, zero or more
[\.] - A literal dot, required
[0-9]{2} - A digit, twice required
这与上面的最小可匹配字符串完全相同-\。\d\d
编辑:如前所述,根据您可能需要转义前斜杠的语言,以确保它们在处理字符串时不会被语言误解
另外,作为旁白,下面的正则表达式可能更接近您所需要的
[A-Z]{3} ?(\d{0,3}(?:([,. ])\d{3}(?:\2\d{3})*)?)(?!\2)[,.](\d\d)\b
说明:
[A-Z]{3} - Three letters; for an ISO currency code
? - A space, zero or more; for readability
( - Capture block; to catch the integer currency amount
\d{0,3} - A digit, between one and three; for the first digit block
(?: - Non capturing block (NC)
([,. ]) - A comma, dot or space; as a thousands delimiter
\d{3} - A digit, three; the first possible whole thousands
(?: - Non capturing block (NC)
\2 - Match 2; the captured thousands delimiter above
\d{3} - A digits, three
)* - The above group, zero or more, i.e. as many thousands as we want
)? - The above (NC) group, zero or one, ie. all whole thousands
) - The above group, i.e everything before the decimal
[.,] - A comma or dot, as a decimal delimiter
(\d{2}) - Capture, A digit, two; ie. the decimal portion
\b - A word boundry; to ensure that we don't catch another
digit in the wrong place.
年约翰·库格曼(John Kugelman)的回答提供了负面展望
这正确匹配(匹配项括在方括号内):
但不是:
GBP 1.000
YEN 200,000
示例脚本中使用的“正则表达式”不是RegExp:
$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
相反,它是一个字符串,其中包含一个模式,该模式在某个点上被库使用类似于
var RE=!(value instanceof RegExp) ? new RegExp(value) : value;
在字符串中,反斜杠\
用于表示特殊字符,如\n
表示新行。在句点的开头添加反斜杠,即\.
,没有任何作用,因为不需要“转义”句点
因此,从字符串创建的RegExp根本看不到反斜杠
使用JavaScript的文本正则表达式分隔符,而不是提供字符串作为正则表达式
因此,不是:
$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
使用
我相信你的“正则表达式”会像你期望的那样运行
(请注意,使用前斜杠/
来界定模式,JavaScript将使用它来提供真正的RegExp。)示例脚本中使用的“正则表达式”不是RegExp:
$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
相反,它是一个字符串,其中包含一个模式,该模式在某个点上被库使用类似于
var RE=!(value instanceof RegExp) ? new RegExp(value) : value;
在字符串中,反斜杠\
用于表示特殊字符,如\n
表示新行。在句点的开头添加反斜杠,即\.
,没有任何作用,因为不需要“转义”句点
因此,从字符串创建的RegExp根本看不到反斜杠
使用JavaScript的文本正则表达式分隔符,而不是提供字符串作为正则表达式
因此,不是:
$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
使用
我相信你的“正则表达式”会像你期望的那样运行
(注意使用前斜杠
/
来界定模式,JavaScript将使用它来提供真正的RegExp。)您错了,或者缺少上下文。\.
中的反斜杠可能被“吃掉”通过您使用的任何软件。您需要提供您的环境、失败代码等的详细信息。您使用的是什么工具或语言,您所说的是不可能的,除非您使用的工具对此有所说明:)正则表达式不尊重用户的区域设置。例如,输入字符串€4.655,00在欧洲部分地区是有效的货币值-这就是为什么如果您的语言支持,最好使用区域设置支持。我们需要有关您环境的更多详细信息;这是L吗