en.javascript.info/9-regular-expressions/09-regexp-groups/3-find-decimal-positive-numbers/solution.md
Ilya Kantor 4f2a90854d fixes
2019-06-25 16:25:50 +03:00

491 B

An non-negative integer number is pattern:\d+. A zero 0 can't be the first digit, but we should allow it in further digits.

So that gives us pattern:[1-9]\d*.

A decimal part is: pattern:\.\d+.

Because the decimal part is optional, let's put it in parentheses with the quantifier pattern:?.

Finally we have the regexp: pattern:[1-9]\d*(\.\d+)?:

let reg = /[1-9]\d*(\.\d+)?/g;

let str = "1.5 0 -5 12. 123.4.";

alert( str.match(reg) );   // 1.5, 0, 12, 123.4