en.javascript.info/9-regular-expressions/11-regexp-groups/03-find-decimal-numbers/solution.md
2020-12-09 18:54:47 +02:00

259 B

A positive number with an optional decimal part is: pattern:\d+(\.\d+)?.

Let's add the optional pattern:- in the beginning:

let regexp = /-?\d+(\.\d+)?/g;

let str = "-1.5 0 2 -123.4.";

alert( str.match(regexp) );   // -1.5, 0, 2, -123.4