简体   繁体   English

美元货币格式的正则表达式

[英]Regex for dollar currency format

I need a regex to check if the value is valid dollar currency我需要一个正则表达式来检查该值是否是有效的美元货币

eg 12,123,212.00例如 12,123,212.00

  • It should accept "," as per the dollar format XXX,XXX,XXX它应该按照美元格式 XXX,XXX,XXX 接受“,”
  • Should accept only one decimal, only two digit after decimal应该只接受一位小数,只接受小数点后两位
  • Value can also be without "," eg 12123212.00值也可以不带“,”,例如 12123212.00

You can try this...你可以试试这个...

 ^\$?((\d{1,3})(?:,[0-9]{3}){0,1}|(\d{1})(?:,[0-9]{3}){0,2}|(\d{1,7}))(\.\d{1,2})?$

If you really want to use regex to validate your input then you may consider:如果您真的想使用正则表达式来验证您的输入,那么您可以考虑:

/^\$?\d{1,3}(?:,\d{3})*(?:\.\d{0,2})?$/gm

RegEx Demo正则表达式演示

Other answers miss your requirement Value can also be without "," eg 12123212.00其他答案未达到您的要求Value can also be without "," eg 12123212.00

So I think you should go with:所以我认为你应该 go 与:

^(\d+|\d{1,3}(,\d{3})+)\.\d{2}$

Demo: https://regex101.com/r/FkWLjr/3演示: https://regex101.com/r/FkWLjr/3

Feel free to overload the syntax with non capturing groups if the capturing groups bother you.如果捕获组打扰您,请随意使用非捕获组重载语法。

If you want to allow amounts without decimal parts, just change \.\d{2}$ to (\.\d{2})?$ .如果您想允许没有小数部分的金额,只需将\.\d{2}$更改为(\.\d{2})?$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM