JavaScript / ECMAScript

Decimal Number in JS

Matches decimal numbers, including integers and negatives.

Try it in the JS tester →

Pattern

regexJS
^-?\d+(\.\d+)?$

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("^-?\\d+(\\.\\d+)?$", "");
const input = "3.14";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern 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

Same pattern, other engines

← Back to Decimal Number overview (all engines)