/^/
MediumNumbers4 min read

Float Number Regex

Validates a floating-point literal that always contains a decimal point, allowing the digits before or after it to be omitted, similar to how many programming languages parse float literals.

#float#numbers#validation#floating-point#programming

Regex Pattern

^-?(\d+\.\d*|\.\d+)$

Pattern Breakdown

Hover over a token to see what it does.

^-?(\d+\.\d*|\.\d+)$
TokenMeaning
^Anchors the match to the start of the string.
-?An optional leading minus sign for negative values.
\d+\.\d*One or more digits, a decimal point, then zero or more digits (covers '3.' and '3.14').
\.\d+A decimal point followed by one or more digits, with no leading integer part (covers '.5').
$Anchors the match to the end of the string.

Detailed Explanation

What it does

This pattern matches floating-point literals as many programming languages write them, always containing a decimal point but allowing either the integer part or the fractional part to be omitted. It accepts forms like 3.14, 3., and .5, but rejects a bare integer like 42 since that has no decimal point at all.

Why it works

The two alternatives split the problem in half: `\d+\.\d*` requires digits before the point and allows zero or more after it (so a trailing point alone is fine), while `\.\d+` requires digits after the point when there are none before it. Together they cover every valid placement of the decimal point except the case with no point at all, which is deliberately excluded so this pattern stays distinct from a plain integer.

Common use cases

  • Validating source code literals or config values that follow a language's float syntax
  • Parsing numeric tokens in a lightweight lexer or template engine
  • Accepting user input for float fields where trailing or leading decimal points are common typing patterns
  • Distinguishing a floating-point literal from an integer literal during static analysis

Edge cases

  • 3. matches because a trailing decimal point with no digits after it is valid float syntax in many languages
  • .5 matches because a leading decimal point with no digits before it is also valid float syntax
  • 42 fails because it has no decimal point, making it an integer rather than a float literal under this definition
  • A lone dot (.) fails because neither alternative is satisfied: there are no digits on either required side

Limitations

  • Does not accept scientific notation such as 1.5e10; combine with an exponent suffix for that
  • Does not treat plain integers as floats, which may differ from languages where 3 is a valid float literal
  • Does not support special float values like Infinity or NaN

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

3.14 3. .5

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const floatPattern = /^-?(\d+\.\d*|\.\d+)$/;

function isFloatLiteral(value) {
  return floatPattern.test(value);
}

console.log(isFloatLiteral(".5")); // true

Common Mistakes

Confusing this pattern with decimal-number, then being surprised that '3.' or '.5' fail under the stricter pattern.

Fix: Use float-number when leading or trailing digits around the point may be omitted, and decimal-number when both sides are always required.

Expecting a plain integer like 42 to match as a float.

Fix: Combine this pattern with the integer pattern via alternation if both integer and float literals should be accepted.

Forgetting that scientific notation is out of scope, so 1e10 unexpectedly fails.

Fix: Use the dedicated scientific-notation pattern, or extend this one with an optional exponent suffix.

Performance Notes

  • The two alternatives are mutually exclusive based on whether digits precede the decimal point, so the engine rarely backtracks between them.
  • Anchoring both ends lets malformed input fail fast without scanning further.
  • Bounded quantifiers (`+`, `*` on non-overlapping classes) keep matching at linear time.

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes