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
Copy file name to clipboardExpand all lines: 9-regular-expressions/06-regexp-boundary/article.md
+10-11
Original file line number
Diff line number
Diff line change
@@ -25,29 +25,28 @@ So, it matches the pattern `pattern:\bHello\b`, because:
25
25
26
26
1. At the beginning of the string matches the first test `pattern:\b`.
27
27
2. Then matches the word `pattern:Hello`.
28
-
3. Then the test `pattern:\b`- matches again, as we're between `subject:o` and a space.
28
+
3. Then the test `pattern:\b` matches again, as we're between `subject:o` and a space.
29
29
30
-
Шаблон `pattern:\bJava\b`также совпадёт. Но не`pattern:\bHell\b` (потому что после `subject:l` нет границы слова), и не `pattern:Java!\b` (восклицательный знак не является "символом слова" `pattern:\w`, поэтому после него нет границы слова).
30
+
The pattern `pattern:\bJava\b`would also match. But not`pattern:\bHell\b` (because there's no word boundary after `l`) and not `Java!\b` (because the exclamation sign is not a wordly character `pattern:\w`, so there's no word boundary after it).
alert( "Hello, Java!".match(/\bHell\b/) ); // null (no match)
36
+
alert( "Hello, Java!".match(/\bJava!\b/) ); // null (no match)
37
37
```
38
38
39
-
Так как `pattern:\b`является проверкой, то не добавляет символ после границы к результату.
39
+
We can use `pattern:\b`not only with words, but with digits as well.
40
40
41
-
Мы можем использовать `pattern:\b` не только со словами, но и с цифрами.
42
-
43
-
Например, регулярное выражение `pattern:\b\d\d\b` ищет отдельно стоящие двузначные числа. Другими словами, оно требует, чтобы до и после `pattern:\d\d` был символ, отличный от `pattern:\w` (или начало/конец строки)
41
+
For example, the pattern `pattern:\b\d\d\b` looks for standalone 2-digit numbers. In other words, it looks for 2-digit numbers that are surrounded by characters different from `pattern:\w`, such as spaces or punctuation (or text start/end).
```warn header="Граница слова`pattern:\b`не работает для алфавитов, не основанных на латинице"
50
-
Проверка границы слова `pattern:\b`проверяет границу, должно быть `pattern:\w`с одной стороны и "не`pattern:\w`" - с другой.
48
+
```warn header="Word boundary`pattern:\b`doesn't work for non-latin alphabets"
49
+
The word boundary test `pattern:\b`checks that there should be `pattern:\w`on the one side from the position and "not`pattern:\w`" - on the other side.
51
50
52
-
Но`pattern:\w`означает латинскую букву (или цифру или знак подчёркивания), поэтому проверка не будет работать для других символов (например, кириллицы или иероглифов).
51
+
But`pattern:\w`means a latin letter `a-z` (or a digit or an underscore), so the test doesn't work for other characters, e.g. cyrillic letters or hieroglyphs.
Copy file name to clipboardExpand all lines: 9-regular-expressions/07-regexp-escaping/article.md
+13-13
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
2
2
# Escaping, special characters
3
3
4
-
As we've seen, a backslash `"\"` is used to denote character classes. So it's a special character in regexps (just like in a regular string).
4
+
As we've seen, a backslash `pattern:\` is used to denote character classes, e.g. `pattern:\d`. So it's a special character in regexps (just like in regular strings).
5
5
6
6
There are other special characters as well, that have special meaning in a regexp. They are used to do more powerful searches. Here's a full list of them: `pattern:[ \ ^ $ . | ? * + ( )`.
7
7
8
8
Don't try to remember the list -- soon we'll deal with each of them separately and you'll know them by heart automatically.
9
9
10
10
## Escaping
11
11
12
-
Let's say we want to find a dot literally. Not "any character", but just a dot.
12
+
Let's say we want to find literally a dot. Not "any character", but just a dot.
13
13
14
14
To use a special character as a regular one, prepend it with a backslash: `pattern:\.`.
15
15
@@ -43,11 +43,11 @@ Here's what a search for a slash `'/'` looks like:
43
43
alert( "/".match(/\//) ); // '/'
44
44
```
45
45
46
-
On the other hand, if we're not using `/.../`, but create a regexp using `new RegExp`, then we don't need to escape it:
46
+
On the other hand, if we're not using `pattern:/.../`, but create a regexp using `new RegExp`, then we don't need to escape it:
47
47
48
48
```js run
49
-
alert( "/".match(newRegExp("/")) ); //'/'
50
-
```
49
+
alert( "/".match(newRegExp("/")) ); //finds /
50
+
```
51
51
52
52
## new RegExp
53
53
@@ -61,25 +61,25 @@ let reg = new RegExp("\d\.\d");
61
61
alert( "Chapter 5.1".match(reg) ); // null
62
62
```
63
63
64
-
The search worked with `pattern:/\d\.\d/`, but with `new RegExp("\d\.\d")` it doesn't work, why?
64
+
The similar search in one of previous examples worked with `pattern:/\d\.\d/`, but `new RegExp("\d\.\d")` doesn't work, why?
65
65
66
-
The reason is that backslashes are "consumed" by a string. Remember, regular strings have their own special characters like`\n`, and a backslash is used for escaping.
66
+
The reason is that backslashes are "consumed" by a string. As we may recall, regular strings have their own special characters, such as`\n`, and a backslash is used for escaping.
67
67
68
-
Please, take a look, what "\d\.\d" really is:
68
+
Here's how "\d\.\d" is preceived:
69
69
70
70
```js run
71
71
alert("\d\.\d"); // d.d
72
72
```
73
73
74
-
The quotes "consume" backslashes and interpret them, for instance:
74
+
String quotes "consume" backslashes and interpret them on their own, for instance:
75
75
76
76
-`\n` -- becomes a newline character,
77
77
-`\u1234` -- becomes the Unicode character with such code,
78
78
- ...And when there's no special meaning: like `pattern:\d` or `\z`, then the backslash is simply removed.
79
79
80
-
So the call to `new RegExp` gets a string without backslashes. That's why the search doesn't work!
80
+
So `new RegExp` gets a string without backslashes. That's why the search doesn't work!
81
81
82
-
To fix it, we need to double backslashes, because quotes turn `\\` into `\`:
82
+
To fix it, we need to double backslashes, because string quotes turn `\\` into `\`:
0 commit comments