reg->regexp
This commit is contained in:
parent
4232a53219
commit
32e20fc97c
35 changed files with 132 additions and 132 deletions
|
@ -6,11 +6,11 @@ We can exclude negatives by prepending it with the negative lookahead: `pattern:
|
|||
Although, if we try it now, we may notice one more "extra" result:
|
||||
|
||||
```js run
|
||||
let reg = /(?<!-)\d+/g;
|
||||
let regexp = /(?<!-)\d+/g;
|
||||
|
||||
let str = "0 12 -5 123 -18";
|
||||
|
||||
console.log( str.match(reg) ); // 0, 12, 123, *!*8*/!*
|
||||
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.
|
||||
|
@ -20,9 +20,9 @@ We can do it by specifying another negative lookbehind: `pattern:(?<!-)(?<!\d)\d
|
|||
We can also join them into a single lookbehind here:
|
||||
|
||||
```js run
|
||||
let reg = /(?<![-\d])\d+/g;
|
||||
let regexp = /(?<![-\d])\d+/g;
|
||||
|
||||
let str = "0 12 -5 123 -18";
|
||||
|
||||
alert( str.match(reg) ); // 0, 12, 123
|
||||
alert( str.match(regexp) ); // 0, 12, 123
|
||||
```
|
||||
|
|
|
@ -6,9 +6,9 @@ Create a regexp that looks for only non-negative ones (zero is allowed).
|
|||
|
||||
An example of use:
|
||||
```js
|
||||
let reg = /your regexp/g;
|
||||
let regexp = /your regexp/g;
|
||||
|
||||
let str = "0 12 -5 123 -18";
|
||||
|
||||
alert( str.match(reg) ); // 0, 12, 123
|
||||
alert( str.match(regexp) ); // 0, 12, 123
|
||||
```
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
Например:
|
||||
|
||||
```js
|
||||
let reg = /ваше регулярное выражение/;
|
||||
let regexp = /ваше регулярное выражение/;
|
||||
|
||||
let str = `
|
||||
<html>
|
||||
|
@ -17,7 +17,7 @@ let str = `
|
|||
</html>
|
||||
`;
|
||||
|
||||
str = str.replace(reg, `<h1>Hello</h1>`);
|
||||
str = str.replace(regexp, `<h1>Hello</h1>`);
|
||||
```
|
||||
|
||||
После этого значение `str`:
|
||||
|
|
|
@ -96,18 +96,18 @@ In the example below the currency sign `pattern:(€|kr)` is captured, along wit
|
|||
|
||||
```js run
|
||||
let str = "1 turkey costs 30€";
|
||||
let reg = /\d+(?=(€|kr))/; // extra parentheses around €|kr
|
||||
let regexp = /\d+(?=(€|kr))/; // extra parentheses around €|kr
|
||||
|
||||
alert( str.match(reg) ); // 30, €
|
||||
alert( str.match(regexp) ); // 30, €
|
||||
```
|
||||
|
||||
And here's the same for lookbehind:
|
||||
|
||||
```js run
|
||||
let str = "1 turkey costs $30";
|
||||
let reg = /(?<=(\$|£))\d+/;
|
||||
let regexp = /(?<=(\$|£))\d+/;
|
||||
|
||||
alert( str.match(reg) ); // 30, $
|
||||
alert( str.match(regexp) ); // 30, $
|
||||
```
|
||||
|
||||
## Summary
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue