Float / Scientific Number in JS
Match floating-point and scientific-notation numbers including `1.5`, `.25`, `1e10`, `-3.14E-2`.
Try it in the JS tester →Pattern
regexJS
[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)? (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("[-+]?(?:\\d+\\.?\\d*|\\.\\d+)(?:[eE][-+]?\\d+)?", "g");
const input = "h = 6.626e-34, c = 3e8, alpha = .007";
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
[-+]? matches an optional sign. (?:\d+\.?\d*|\.\d+) matches the mantissa as either digits-with-optional-decimal or decimal-with-trailing-digits. (?:[eE][-+]?\d+)? matches the optional exponent. Covers most real-number literals you'll encounter in source or data.
Examples
Input
h = 6.626e-34, c = 3e8, alpha = .007Matches
6.626e-343e8.007
Input
Range -1.5 to +2.5Matches
-1.5+2.5
Input
no numbers hereNo match
—