Numbers
US Currency (USD)
Matches USD currency amounts with optional $ sign, thousands separators, and cents.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^\\$?\\d{1,3}(,\\d{3})*(\\.\\d{2})?$", "");
const input = "$1,234.56";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
pyPython
import re
pattern = re.compile(r"^\$?\d{1,3}(,\d{3})*(\.\d{2})?$")
input_text = "$1,234.56"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^\$?\d{1,3}(,\d{3})*(\.\d{2})?$`)
input := `$1,234.56`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
regexengine-agnostic
^\$?\d{1,3}(,\d{3})*(\.\d{2})?$Raw source: ^\$?\d{1,3}(,\d{3})*(\.\d{2})?$
How it works
`^\$?` optional dollar sign. `\d{1,3}(,\d{3})*` matches the integer part with optional comma-separated thousands. `(\.\d{2})?` optional cents.
Examples
Input
$1,234.56Matches
$1,234.56
Input
999Matches
999
Input
$1000000.00No match
—Common use cases
- •Invoice parsing
- •Price validation
- •E-commerce forms
Related patterns
USD Currency (Inline)
NumbersMatch US dollar amounts inline in text: `$1,234.56`, `$99`, `$1,000,000.00`.
Percentage
NumbersMatches percentage values with optional decimal and a trailing % sign.
Binary Number Literal
NumbersMatch binary number literals like `0b1010` or `0B11110000`.
Decimal Number
NumbersMatches decimal numbers, including integers and negatives.