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: 10-regular-expressions-javascript/02-regexp-methods/article.md
+17-19Lines changed: 17 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ alert( result.input ); // "Fame is the thirst of youth" (the string)
48
48
49
49
The array may have more than one element.
50
50
51
-
**If a part of the pattern is delimited by brackets`(...)`, then it becomes a separate element of the array.**
51
+
**If a part of the pattern is delimited by parentheses`(...)`, then it becomes a separate element of the array.**
52
52
53
53
For instance:
54
54
@@ -58,18 +58,18 @@ lar str = "JavaScript is a programming language";
58
58
let result =str.match( *!*/JAVA(SCRIPT)/i*/!* );
59
59
60
60
alert( result[0] ); // JavaScript (the whole match)
61
-
alert( result[1] ); // script (the part of the match that corresponds to the brackets)
61
+
alert( result[1] ); // script (the part of the match that corresponds to the parentheses)
62
62
alert( result.index ); // 0
63
63
alert( result.input ); // JavaScript is a programming language
64
64
```
65
65
66
66
Due to the `i` flag the search is case-insensitive, so it finds `match:JavaScript`. The part of the match that corresponds to `pattern:SCRIPT` becomes a separate array item.
67
67
68
-
We'll be back to brackets later in the chapter [todo]. They are great for search-and-replace.
68
+
We'll be back to parentheses later in the chapter <info:regexp-groups>. They are great for search-and-replace.
69
69
70
70
## str.match(reg) with "g" flag
71
71
72
-
When there's a `"g"` flag, then `str.match` returns an array of all matches. There are no additional properties in that array, and brackets do not create any elements.
72
+
When there's a `"g"` flag, then `str.match` returns an array of all matches. There are no additional properties in that array, and parentheses do not create any elements.
73
73
74
74
For instance:
75
75
@@ -81,9 +81,7 @@ let result = str.match( *!*/ho/ig*/!* );
81
81
alert( result ); // HO, Ho, ho (all matches, case-insensitive)
82
82
```
83
83
84
-
With brackets nothing changes, here we go:
85
-
86
-
84
+
With parentheses nothing changes, here we go:
87
85
88
86
```js run
89
87
let str ="HO-Ho-ho!";
@@ -95,7 +93,7 @@ alert( result ); // HO, Ho, ho
95
93
96
94
So, with `g` flag the `result` is a simple array of matches. No additional properties.
97
95
98
-
If we want to get information about match positions and use brackets then we should use [RegExp#exec](mdn:js/RegExp/exec) method that we'll cover below.
96
+
If we want to get information about match positions and use parentheses then we should use [RegExp#exec](mdn:js/RegExp/exec) method that we'll cover below.
99
97
100
98
````warn header="If there are no matches, the call to `match` returns `null`"
101
99
Please note, that's important. If there were no matches, the result is not an empty array, but `null`.
@@ -155,7 +153,7 @@ We can use special characters in it:
155
153
|`$&`|the whole match|
156
154
|<code>$`</code>|a part of the string before the match|
157
155
|`$'`|a part of the string after the match|
158
-
|`$n`|if `n` is a 1-2 digit number, then it means the contents of n-th brackets counting fro left to right|
156
+
|`$n`|if `n` is a 1-2 digit number, then it means the contents of n-th parentheses counting fro left to right|
159
157
160
158
For instance let's use `$&` to replace all entries of`"John"` by `"Mr.John"`:
In the example below there are two brackets, so `replacer` is called with 5 arguments: `str` is the full match, then brackets, and then `offset` and `s`:
220
+
In the example below there are two parentheses, so `replacer` is called with 5 arguments: `str` is the full match, then parentheses, and then `offset` and `s`:
223
221
224
222
```js run
225
223
functionreplacer(str, name, surname, offset, s) {
226
-
// name is the first bracket, surname is the second one
224
+
// name is the first parentheses, surname is the second one
From the other hand, the alternative `new RegExp` syntaxes does not require escaping it:
47
47
48
48
```js run
49
-
alert( "/".match(newRegExp()/\//) ); // '/'
49
+
alert( "/".match(newRegExp("/")) ); // '/'
50
+
```
51
+
52
+
## new RegExp
53
+
54
+
If we are creating a regular expression with `new RegExp`, then we need to do some more escaping.
55
+
56
+
For instance, consider this:
57
+
58
+
```js run
59
+
let reg =newRegExp("\d\.\d");
60
+
61
+
alert( "Chapter 5.1".match(reg) ); // null
62
+
```
63
+
64
+
It doesn't work, but why?
65
+
66
+
The reason is string escaping rules. Look here:
67
+
68
+
```js run
69
+
alert("\d\.\d"); // d.d
70
+
```
71
+
72
+
Backslashes are used for escaping inside a string and string-specific special characters like `\n`. The quotes "consume" and interpret them, for instance:
73
+
74
+
-`\n` -- becomes a newline character,
75
+
-`\u1234` -- becomes the Unicode character with such code,
76
+
- ...And when there's no special meaning: like `\d` or `\z`, then the backslash is simply removed.
77
+
78
+
So the call to `new RegExp` gets a string without backslashes.
79
+
80
+
To fix it, we need to double backslashes, because quotes turn `\\` into `\`:
alert( "Breakfast at 09:00. Dinner at 21-30".match(reg) ); // 09:00, 21-30
6
+
```
7
+
8
+
Please note that the dash `pattern:'-'` has a special meaning in square brackets, but only between other characters, not when it's in the beginning or at the end, so we don't need to escape it.
0 commit comments