Regex to Extract Numbers from a String — Integers, Decimals, Negatives
Regex patterns to extract numbers from text: integers, decimals, negatives, scientific notation, and thousand-separated values. Includes JavaScript examples.
Detailed Explanation
Extracting Numbers from Strings
Pulling numeric values out of mixed text is a frequent task in scraping, log analysis, and free-form input parsing. The right pattern depends on whether you need integers only, signed numbers, decimals, or formatted values like 1,234.56.
Unsigned Integers
\d+
"order #1234 from 2024".match(/\d+/g) returns ["1234", "2024"].
Signed Integers and Decimals
-?\d+(?:\.\d+)?
Matches 42, -7, 3.14, -0.5.
Scientific Notation
-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?
Matches 1.5e10, -2.3E-7, 6.022e23.
Thousand-Separated Numbers
-?\d{1,3}(?:,\d{3})*(?:\.\d+)?
Matches 1,234, 1,234,567.89, -12,000.
Tested Examples
| Input | Pattern | Matches |
|---|---|---|
"3 cats and 4 dogs" |
\d+ |
3, 4 |
"price -19.99 USD" |
signed decimal | -19.99 |
"radius=6.371e3 km" |
scientific | 6.371e3 |
"revenue: $1,234,567.89" |
thousand-sep | 1,234,567.89 |
"v2.10.3" |
unsigned int | 2, 10, 3 |
Conversion to Numbers
After extraction, convert and remove formatting:
const nums = "$1,234.56"
.match(/-?\d{1,3}(?:,\d{3})*(?:\.\d+)?/g)
?.map(s => Number(s.replace(/,/g, "")));
Word Boundary Trick
To avoid matching numbers embedded in identifiers (abc123), use \b\d+\b. To extract numbers but not version-like sequences, add a negative lookahead: \b\d+\b(?!\.).
Locale Considerations
European locales use . as thousands separator and , as decimal. Swap them in the pattern accordingly, or normalize the input first.
Use Case
Pulling order quantities and prices out of email confirmations, parsing free-form survey responses, or extracting numeric metrics from log lines that mix prose and data.