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.14Matches
3.14
Input
-0.001Matches
-0.001
Input
42Matches
42