regexp task

This commit is contained in:
Ilya Kantor 2019-07-24 11:58:06 +03:00
parent 03560a7f03
commit 5e8edafbe1
6 changed files with 43 additions and 31 deletions

View file

@ -1,4 +1,4 @@
# Find all numbers
# Find decimal numbers
Write a regexp that looks for all decimal numbers including integer ones, with the floating point and negative ones.

View file

@ -1,18 +0,0 @@
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+)?`:
```js run
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
```

View file

@ -1,12 +0,0 @@
# Find positive numbers
Create a regexp that looks for positive numbers, including those without a decimal point.
An example of use:
```js
let reg = /your regexp/g;
let str = "1.5 0 -5 12. 123.4.";
alert( str.match(reg) ); // 1.5, 12, 123.4 (ignores 0 and -5)
```