regexp task
This commit is contained in:
parent
03560a7f03
commit
5e8edafbe1
6 changed files with 43 additions and 31 deletions
|
@ -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.
|
Write a regexp that looks for all decimal numbers including integer ones, with the floating point and negative ones.
|
||||||
|
|
|
@ -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
|
|
||||||
```
|
|
|
@ -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)
|
|
||||||
```
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
|
||||||
|
A number is `pattern:\d+`.
|
||||||
|
|
||||||
|
We can exclude negatives by prepending it with the negative lookahead: `pattern:(?<!-)\d+`.
|
||||||
|
|
||||||
|
If we try it now, we may notice one more "extra" result:
|
||||||
|
|
||||||
|
```js run
|
||||||
|
let reg = /(?<!-)\d+/g;
|
||||||
|
|
||||||
|
let str = "0 12 -5 123 -18";
|
||||||
|
|
||||||
|
console.log( str.match(reg) ); // 0, 12, 123, *!*8*/!*
|
||||||
|
```
|
||||||
|
|
||||||
|
It matches `match:8` of `subject:-18`. To exclude it, we need to ensure that the regexp starts matching a number not from the middle of another number.
|
||||||
|
|
||||||
|
We can do it by specifying another negative lookbehind: `pattern:(?<!-)(?<!\d)\d+`. Now `pattern:(?<!\d)` ensures that a match does not start after another digit.
|
||||||
|
|
||||||
|
Or we can use a single lookbehind here:
|
||||||
|
|
||||||
|
```js run
|
||||||
|
let reg = /(?<![-\d])\d+/g;
|
||||||
|
|
||||||
|
let str = "0 12 -5 123 -18";
|
||||||
|
|
||||||
|
alert( str.match(reg) ); // 0, 12, 123
|
||||||
|
```
|
|
@ -0,0 +1,14 @@
|
||||||
|
# Find non-negative integers
|
||||||
|
|
||||||
|
There's a string of integer numbers.
|
||||||
|
|
||||||
|
Create a regexp that looks for only non-negative ones (zero is allowed).
|
||||||
|
|
||||||
|
An example of use:
|
||||||
|
```js
|
||||||
|
let reg = /your regexp/g;
|
||||||
|
|
||||||
|
let str = "0 12 -5 123 -18";
|
||||||
|
|
||||||
|
alert( str.match(reg) ); // 0, 12, 123
|
||||||
|
```
|
Loading…
Add table
Add a link
Reference in a new issue