Regex to Match Numbers with Thousands Separators

Validate numbers formatted with comma thousands separators and optional decimal places. Matches formats like 1,000 and 1,234,567.89. Free regex tester.

Regular Expression

/^-?\d{1,3}(,\d{3})*(\.\d+)?$/

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
-Matches the literal character '-'
?Makes the preceding element optional (zero or one times)
\dMatches any digit (0-9)
{1,3}Matches between 1 and 3 times
(Start of capturing group
,Matches the literal character ','
\dMatches any digit (0-9)
{3}Matches exactly 3 times
)End of group
*Matches the preceding element zero or more times (greedy)
(Start of capturing group
\.Matches a literal dot
\dMatches any digit (0-9)
+Matches the preceding element one or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates numbers formatted with comma-based thousands separators, a common convention in English-speaking countries. Here is the token-by-token breakdown:

^ — Anchors the match at the start of the string.

-? — Optionally matches a minus sign for negative numbers.

\d{1,3} — Matches one to three digits at the beginning of the number. This is the leading group before the first comma. Numbers can start with 1 digit (like 1,000), 2 digits (like 12,000), or 3 digits (like 123,000).

(,\d{3})* — Capturing group that matches zero or more comma-separated groups of exactly three digits. Each group consists of a comma followed by exactly three digits. This enforces correct placement of thousands separators: every three digits from right to left.

(.\d+)? — An optional capturing group for the decimal portion. It matches a literal dot followed by one or more digits. The decimal portion does not use thousands separators.

$ — Anchors the match at the end of the string.

No flags are used since this validates a single number string.

Thousands separators make large numbers more readable. For example, 1000000 becomes 1,000,000 and 1234567.89 becomes 1,234,567.89. This format is standard in the United States, United Kingdom, and many other countries.

This pattern is useful for validating user input in financial forms, currency fields, data import tools, and spreadsheet applications. For European-style formatting where dots are separators and commas are decimal marks, the characters would need to be swapped.

Example Test Strings

InputExpected
1,000Match
1,234,567.89Match
1234567No Match
12,34No Match
-999,999.00Match
100Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 28 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →