Numbers

Decimal Number

Matches decimal numbers, including integers and negatives.

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^-?\\d+(\\.\\d+)?$", "");
const input = "3.14";
const match = input.match(re);
console.log(match ? match[0] : null);

Uses `String.prototype.match`, which returns the first match or null.

Pattern

regexengine-agnostic
^-?\d+(\.\d+)?$

Raw source: ^-?\d+(\.\d+)?$

How it works

`^-?\d+` matches optional sign and integer part. `(\.\d+)?` optionally matches a decimal point and fractional digits.

Examples

Input

3.14

Matches

  • 3.14

Input

-0.001

Matches

  • -0.001

Input

42

Matches

  • 42

Common use cases

  • •Price input
  • •Scientific data
  • •Form validation

Related patterns

Related concepts