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: 09_regexp.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -134,10 +134,10 @@ These backslash codes can also be used inside ((square brackets)). For example,
134
134
To _invert_ a set of characters—that is, to express that you want to match any character _except_ the ones in the set—you can write a caret (`^`) character after the opening bracket.
135
135
136
136
```
137
-
let notBinary = /[^01]/;
138
-
console.log(notBinary.test("1100100010100110"));
137
+
let nonBinary = /[^01]/;
138
+
console.log(nonBinary.test("1100100010100110"));
139
139
// → false
140
-
console.log(notBinary.test("0111010112101001"));
140
+
console.log(nonBinary.test("0111010112101001"));
141
141
// → true
142
142
```
143
143
@@ -160,7 +160,7 @@ It is possible to use `\p` in a regular expression to match all characters to wh
160
160
| `\p{L}` | Any letter
161
161
| `\p{N}` | Any numeric character
162
162
| `\p{P}` | Any punctuation character
163
-
| `\P{L}` | Any non-letter
163
+
| `\P{L}` | Any non-letter (uppercase P inverts)
164
164
| `\p{Script=Hangul}` | Any character from the given script (see [Chapter ?](higher_order#scripts))
165
165
166
166
Using `\w` for text processing that may need to handle non-English text (or even English text with borrowed words like “cliché”) is a liability, since it won't treat characters like “é” as letters. Though they tend to be a bit more verbose, `\p` property groups are more robust.
@@ -292,7 +292,7 @@ console.log(quotedText.exec("she said 'hello'"));
292
292
293
293
{{index "capture group"}}
294
294
295
-
When a group does not end up being matched at all (for example, when followed by a question mark), its position in the output array will hold `undefined`. Similarly, when a group is matched multiple times, only the last match ends up in the array.
295
+
When a group does not end up being matched at all (for example, when followed by a question mark), its position in the output array will hold `undefined`. And when a group is matched multiple times (for example when it is followed by a `+`), only the last match ends up in the array.
296
296
297
297
```
298
298
console.log(/bad(ly)?/.exec("bad"));
@@ -322,7 +322,7 @@ JavaScript has a standard class for representing ((date))s—or, rather, points
322
322
323
323
```{test: no}
324
324
console.log(new Date());
325
-
// → Mon Nov 13 2017 16:19:11 GMT+0100 (CET)
325
+
// → Fri Feb 02 2024 18:03:06 GMT+0100 (CET)
326
326
```
327
327
328
328
{{index "Date class"}}
@@ -395,7 +395,7 @@ There is also a `\b` marker, which matches “word boundaries”, positions that
395
395
396
396
Note that these markers don't match any actual characters. They just enforces that a given condition holds at the place where they appears in the pattern.
397
397
398
-
{{index "look-ahead}}
398
+
{{index "look-ahead"}}
399
399
400
400
_Look-ahead_ tests do something similar. They provide a pattern, and will make the match fail if the input doesn't match that pattern, but don't actually move the match position forward. They are written between `(?=` and `)`.
401
401
@@ -860,7 +860,7 @@ Regular expressions are a sharp ((tool)) with an awkward handle. They simplify s
860
860
861
861
{{index debugging, bug}}
862
862
863
-
It is almost unavoidable that, in the course of working on these exercises, you will get confused and frustrated by some regular expression's inexplicable ((behavior)). Sometimes it helps to enter your expression into an online tool like [_https://debuggex.com_](https://www.debuggex.com/) to see whether its visualization corresponds to what you intended and to ((experiment)) with the way it responds to various input strings.
863
+
It is almost unavoidable that, in the course of working on these exercises, you will get confused and frustrated by some regular expression's inexplicable ((behavior)). Sometimes it helps to enter your expression into an online tool like [_debuggex.com_](https://www.debuggex.com/) to see whether its visualization corresponds to what you intended and to ((experiment)) with the way it responds to various input strings.
864
864
865
865
### Regexp golf
866
866
@@ -870,7 +870,7 @@ _Code golf_ is a term used for the game of trying to express a particular progra
870
870
871
871
{{index boundary, matching}}
872
872
873
-
For each of the following items, write a ((regular expression)) to test whether any of the given substrings occur in a string. The regular expression should match only strings containing one of the substrings described. When your expression works, see whether you can make it any smaller.
873
+
For each of the following items, write a ((regular expression)) to test whether the given pattern occurs in a string. The regular expression should match only strings containing the pattern. When your expression works, see whether you can make it any smaller.
0 commit comments