Back to the lesson

Find positive numbers

Create a regexp that looks for positive numbers, including those without a decimal point.

An example of use:

let reg = /your regexp/g;

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

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

An integer number is \d+.

A decimal part is: \.\d+.

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

Finally we have the regexp: \d+(\.\d+)?:

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

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

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