en.javascript.info/9-regular-expressions/14-regexp-lookahead-lookbehind/1-find-non-negative-integers/solution.md
2019-09-06 16:50:41 +03:00

28 lines
887 B
Markdown

The regexp for an integer number is `pattern:\d+`.
We can exclude negatives by prepending it with the negative lookahead: `pattern:(?<!-)\d+`.
Although, if we try it now, we may notice one more "extra" result:
```js run
let regexp = /(?<!-)\d+/g;
let str = "0 12 -5 123 -18";
console.log( str.match(regexp) ); // 0, 12, 123, *!*8*/!*
```
As you can see, it matches `match:8`, from `subject:-18`. To exclude it, we need to ensure that the regexp starts matching a number not from the middle of another (non-matching) 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, just what we need.
We can also join them into a single lookbehind here:
```js run
let regexp = /(?<![-\d])\d+/g;
let str = "0 12 -5 123 -18";
alert( str.match(regexp) ); // 0, 12, 123
```