Regex to Extract Numbers From a String

Extract all numbers from text with the global regex \d+. See the matches, then extend the pattern for decimals and negative numbers.

Part of the free Regex Tester. Runs in your browser — nothing is uploaded.

Regex to Extract Numbers From a String

The global flag makes a regex return every match instead of just the first. \d+ matches runs of digits, so a sentence containing prices and counts yields each integer.

Worked example: All integers in a sentence

Pattern /\d+/g — text

\d+

Order 4821 shipped 3 items on day 17.

Matches

3 matches: ["4821","3","17"]

Practical tip

For decimals and negatives use -?\d+(?:\.\d+)?. In JavaScript, str.match(/\d+/g) returns an array, or null when nothing matches.

How to use this Regex Tester

  1. 1Use the live tool above — it runs entirely in your browser and never uploads your data.
  2. 2Paste the sample from the worked example, or use your own input.
  3. 3Copy the result, then adapt the same steps in your editor, script, or pipeline.

Regex to Extract Numbers From a String FAQs

How do I extract decimal numbers?

Use -?\d+(?:\.\d+)? with the g flag. It matches optional minus signs, integers and numbers with a fractional part.

Why does match return null?

With the g flag, String.match returns null when nothing matches. Guard with (str.match(re) || []) before using the result.

More Regex Tester pages

Open the full Regex Tester tool →