0

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

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

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

编辑

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

我正在使用将.formatCurrency()输入格式化为货币格式。它可以在这里找到,但它仍然允许我使用字母字符,所以我必须使用在此处$(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();
            });

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

4

2 回答 2

3

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

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

相反,它是一个字符串,其中包含一个模式,该模式在某些时候被您的库使用类似于以下内容的东西转换为真正的正则表达式

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

在字符串中,反斜杠\用于表示特殊字符,例如\n表示换行符。在句号的开头添加反斜杠,即\.,没有任何作用,因为不需要“转义”句号。

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

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

所以而不是:

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

利用

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

而且我相信您的“正则表达式”会按您的预期执行。

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

于 2013-09-05T23:35:50.607 回答
1

首先,您可以将 '[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 在这个问题中的回答提供了否定的前瞻。

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

[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
于 2013-09-05T23:25:50.853 回答