最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex For digit count in an alphanumeric string - Stack Overflow

programmeradmin0浏览0评论

How can i validate following logic from regex

  • Allow Dollar($),Comma(,),Decimal(.) and digit(0-9)
  • Can have only 8 digit before Decimal irrespective of $ and ma
  • 2 digit after decimal

Allowed string Example:

  • 99999999.99
  • $99999999.99
  • $9,999,9999.99
  • $9999,99,99.99

Does not allow : - $999999999.99 ( 9 digit before decimal) - $99,99,999,99.99

Mean i want to restrict the count of digit only before decimal. How can i achieve this. Thanks in Advance

How can i validate following logic from regex

  • Allow Dollar($),Comma(,),Decimal(.) and digit(0-9)
  • Can have only 8 digit before Decimal irrespective of $ and ma
  • 2 digit after decimal

Allowed string Example:

  • 99999999.99
  • $99999999.99
  • $9,999,9999.99
  • $9999,99,99.99

Does not allow : - $999999999.99 ( 9 digit before decimal) - $99,99,999,99.99

Mean i want to restrict the count of digit only before decimal. How can i achieve this. Thanks in Advance

Share Improve this question asked Mar 3, 2015 at 10:47 MayankMayank 1,3925 gold badges25 silver badges45 bronze badges 2
  • Does it have to be done entirely in regexp? – Barmar Commented Mar 3, 2015 at 10:50
  • ^(\$?(\,?\d){1,8}\.\d{2})$ – Vladu Ionut Commented Mar 3, 2015 at 11:06
Add a ment  | 

2 Answers 2

Reset to default 4

You can use this regex:

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

RegEx Demo

Explanation:

^              # Line start
\$?            # match optional $ at start
(?:,?\d)       # Match an optional ma followed by a digit and use non-capturing group
{1,8}          # up to 8 occurrence of previous group
(?:\.\d{1,2})? # followed by optional decimal point and 1 or 2 digits
$              # line end
/^(\$?(\,?\d){1,8}\.\d{2}$)/gm

Regex Demo

发布评论

评论列表(0)

  1. 暂无评论