Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for US dollar amount
Regex pattern · validation

Regex for US dollar amount

Regex for currency: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /^\$?(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/ as a US dollar amount regex. It matches "$1,234.56" and "1234.56", and rejects "$1,23.45" and "$12.3". Store money as integer cents or a decimal type, never as floating point, after matching.

Last updated · Maintained by FreeAIToolz · How we test

/^\$?(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/
  • $1,234.56matches
  • 1234.56matches
  • $0.99matches
  • $100matches
  • $1,23.45no match
  • $12.3no match
  • 12.345no match
  • USD 12no match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
^\$?optional dollar sign
(?:\d{1,3}(?:,\d{3})*|\d+)whole dollars with or without thousands commas
(?:\.\d{2})?$optional cents (exactly two digits)

Examples

InputResult
$1,234.56✓ Matches
1234.56✓ Matches
$0.99✓ Matches
$100✓ Matches
$1,23.45✗ No match
$12.3✗ No match
12.345✗ No match
USD 12✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Store money as integer cents or a decimal type, never as floating point, after matching.

When to use this pattern

Price inputs in invoices, checkout and finance forms.

Use it in your language

JavaScript

const re = /^\$?(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^\$?(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^\\$?(?:\\d{1,3}(?:,\\d{3})*|\\d+)(?:\\.\\d{2})?$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^\\$?(?:\\d{1,3}(?:,\\d{3})*|\\d+)(?:\\.\\d{2})?$");
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`^\$?(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for US dollar amount?
/^\$?(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?$/. Store money as integer cents or a decimal type, never as floating point, after matching.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abc$1,234.56xyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.