正则表达式允许一个字符(它不应该)为什么?

您好我正在尝试创建一个识别正在输入的金钱和数字的正则表达式。 我必须允许数字,因为我希望以编程方式输入非格式化的数字,然后我将自己格式化。 出于某种原因,我的正则表达式允许使用单字母字符作为可能的输入。

[\$]?[0-9,]*\.[0-9][0-9] 

我知道我的正则表达式接受添加多个逗号并且小数点后还需要两位数的情况。 我已经知道如何解决这个问题。 我把它缩小到可能的*\. 作为问题

编辑

我发现正则表达式有效[\$]?([0-9,])*[\.][0-9]{2}但我仍然不知道它是如何或为什么失败的第一名

我使用.formatCurrency()将输入格式化为货币格式。 它可以在这里找到,但它仍然允许我使用字母字符,所以我必须使用$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });进一步掩盖它$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" }); 在这里找到输入掩码, $(this)是对text类型的输入元素的引用。 我的代码看起来像这样

  //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(); }); 

我希望这有帮助。 我尝试创建一个JSfiddle但Idk如何添加外部库/插件/扩展

您在示例脚本中使用的“正则表达式”不是RegExp:

 $(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" }); 

相反,它是一个String,它包含一个模式, 在某些时候 ,您的库将使用某些东西将其转换为真正的RegExp。

 var RE=!(value instanceof RegExp) ? new RegExp(value) : value; 

在字符串中,反斜杠\用于表示特殊字符,例如\n表示换行符。 在句点的开头添加反斜杠,即\. 什么都不做,因为没有必要“逃避”这个时期。

因此,从String创建的RegExp根本看不到反斜杠。

不使用String作为正则表达式,而是使用JavaScript的文字正则表达式分隔符。

所以而不是:

 $(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" }); 

使用

 $(this).inputmask('Regex', { regex: /[\$]?([0-9,])*[\.][0-9]{2}/ }); 

而且我相信你的“正则表达”会像你期望的那样表现出来。

(注意使用正斜杠/来分隔您的模式,JavaScript将使用它来提供真正的RegExp。)

首先,您可以将'[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 ,三个字符长。 你给的正则表达式永远不会validation任何一个字符串。

看着你的第二个正则表达式,

 [\$]? - 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

编辑:如上所述,根据您可能需要转义斜杠的语言,以确保在处理字符串时不会被语言误解。

另外,另外,下面的正则表达式可能更接近你需要的。

 [AZ]{3} ?(\d{0,3}(?:([,. ])\d{3}(?:\2\d{3})*)?)(?!\2)[,.](\d\d)\b 

说明:

 [AZ]{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, ie as many thousands as we want )? - The above (NC) group, zero or one, ie. all whole thousands ) - The above group, ie 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的回答提供了负面的预测。

这正确匹配(括号括在方括号中):

 [AUD 1.00] [USD 1,300,000.00] [YEN 200 000.00] I need [USD 1,000,000.00], all in non-sequential bills. 

但不是:

 GBP 1.000 YEN 200,000