Numbers

Regex for Numbers: Integers, Decimals, Positive & Negative

Copy, test, and understand practical number regex patterns—from digits only to signed integers and decimals—without confusing text matching with numeric validation.

Most common pattern: use ^\d+$ when the complete value must contain one or more digits and nothing else.

Regex for numbers only

To accept one or more digits from the start to the end of the string:

^\d+$

Matches 123 and 007. Rejects 12A, -5, and 12.5. If you only want ASCII digits, ^[0-9]+$ is explicit and portable.

Regex for integers

An integer may optionally have a plus or minus sign:

^[+-]?\d+$

Matches 42, -42, and +42. If your requirement does not allow a leading plus sign, use ^-?\d+$.

Positive numbers only

If “positive” means digits without a sign, ^\d+$ accepts zero too. If zero must be rejected, a simple integer pattern is:

^[1-9]\d*$

This matches 1, 25, and 900, but not 0. Decide whether leading zeros such as 007 are valid for your data before choosing the pattern.

Regex for decimal numbers

A practical dot-decimal pattern with an optional sign is:

^[+-]?\d+(?:\.\d+)?$

Matches 10, -10.5, and +0.75. It intentionally rejects values such as .5 and 5.. If those formats are valid in your application, adjust the requirement before expanding the regex.

Limit the number of digits

Quantifiers are useful when the requirement is about length rather than numeric value.

^\d{1,3}$

This accepts one to three digits: 7, 42, and 999. It does not mean “a number from 1 to 999” because it also accepts 000. Numeric ranges are usually clearer after converting the value to a number.

Formatted numbers with commas

For a common US-style thousands separator with an optional decimal part:

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

Examples include 1,250, -10,000.50, and 999. Locale-specific formatting varies, so do not reuse this pattern blindly for every country.

Regex range vs numeric range

Regex is excellent for structure and digit counts. It becomes harder to maintain when you encode arbitrary numeric ranges such as 1–255 or 1900–2099. For application logic, a clearer flow is usually:

const raw = '150';

if (/^\d+$/.test(raw)) {
  const value = Number(raw);
  const valid = value >= 1 && value <= 255;
}

Let regex validate the textual shape, then use numeric comparison for the numeric rule.

Number regex in JavaScript

const integer = /^[+-]?\d+$/;
const decimal = /^[+-]?\d+(?:\.\d+)?$/;

integer.test('-42');   // true
decimal.test('19.99'); // true

For more JavaScript-specific examples, see the JavaScript Regex guide.

Common mistakes with numeric regex

  • Using \d+ without anchors when the whole input must be numeric.
  • Assuming a pattern that looks numeric also enforces a numeric range.
  • Forgetting whether signs, decimals, leading zeros, or thousands separators are allowed.
  • Using one locale's decimal separator for users in another locale.

Test your number regex online

Use the RegexArena Playground to test both values that should match and values that should fail. Then use the exercises to practice writing the pattern from a requirement.

Open Regex TesterPractice Regex Problems

Practice Number Regex

Now solve exercises 01, 02, 06, 16, and 17 without opening the answers first. They cover digits only, exact length, signed integers, and decimals.

Practice number regex

Keep learning

← Back to learning hub