You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lookbehind allows to match a pattern only if there's something before.
35
+
Lookahead allows to add a condition for "what goes after".
36
+
37
+
Lookbehind is similar, but it looks behind. That is, it allows to match a pattern only if there's something before.
34
38
35
39
The syntax is:
36
40
- Positive lookbehind: `pattern:(?<=y)x`, matches `pattern:x`, but only if it follows after `pattern:y`.
37
41
- Negative lookbehind: `pattern:(?<!y)x`, matches `pattern:x`, but only if there's no `pattern:y` before.
38
42
39
-
For example, let's change the price to US dollars. The dollar sign is usually before the number, so to look for `$30` we'll use `pattern:(?<=\$)\d+`:
43
+
For example, let's change the price to US dollars. The dollar sign is usually before the number, so to look for `$30` we'll use `pattern:(?<=\$)\d+` -- an amount preceeded by `subject:$`:
40
44
41
45
```js run
42
46
let str ="1 turkey costs $30";
43
47
44
-
alert( str.match(/(?<=\$)\d+/) ); // 30 (correctly skipped the sole number 1)
48
+
alert( str.match(/(?<=\$)\d+/) ); // 30 (skipped the sole number)
45
49
```
46
50
47
-
And for the quantity let's use a negative lookbehind `pattern:(?<!\$)\d+`:
51
+
And, to find the quantity -- a number, not preceeded by `subject:$`, we can use a negative lookbehind `pattern:(?<!\$)\d+`:
48
52
49
53
```js run
50
54
let str ="2 turkeys cost $60";
51
55
52
-
alert( str.match(/(?<!\$)\d+/) ); // 2 (correctly skipped the price)
56
+
alert( str.match(/(?<!\$)\d+/) ); // 2 (skipped the price)
53
57
```
54
58
55
59
## Capture groups
56
60
57
61
Generally, what's inside the lookaround (a common name for both lookahead and lookbehind) parentheses does not become a part of the match.
58
62
59
-
But if we want to capture something, that's doable. Just need to wrap that into additional parentheses.
63
+
E.g. in the pattern `pattern:\d+(?!€)`, the `pattern:€` sign doesn't get captured as a part of the match.
64
+
65
+
But if we want to capture the whole lookaround expression or a part of it, that's possible. Just need to wrap that into additional parentheses.
60
66
61
67
For instance, here the currency `pattern:(€|kr)` is captured, along with the amount:
62
68
63
69
```js run
64
70
let str ="1 turkey costs 30€";
65
-
let reg =/\d+(?=(€|kr))/;
71
+
let reg =/\d+(?=(€|kr))/;// extra parentheses around €|kr
`Origin` нужен, потому что `Referer` передаётся не всегда. В частности, при запросе с HTTPS на HTTP -- нет `Referer`.
2
+
3
+
Политика [Content Security Policy](http://en.wikipedia.org/wiki/Content_Security_Policy) может запрещать пересылку `Referer`.
4
+
5
+
По стандарту `Referer` является необязательным HTTP-заголовком, в некоторых браузерах есть настройки, которые запрещают его слать.
6
+
7
+
Именно поэтому, ввиду того, что на `Referer` полагаться нельзя, и придумали заголовок `Origin`, который гарантированно присылается при кросс-доменных запросах.
8
+
9
+
Что же касается "неправильного" `Referer` -- это из области фантастики. Когда-то, много лет назад, в браузерах были ошибки, которые позволяли подменить `Referer` из JavaScript, но они давно исправлены. Никакая "злая страница" не может его подменить.
0 commit comments