Skip to content

Commit 8c09c00

Browse files
updated js regexp cheatsheet
1 parent beb93f6 commit 8c09c00

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

_posts/2019-12-06-javascript-regexp-cheatsheet.md

+27-15
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ tags:
1010
date: 2019-12-06T14:37:42
1111
---
1212

13-
This blog post gives an overview of regular expression syntax and features supported by JavaScript. Examples have been tested on Chrome/Chromium console (version 78+) and includes features not available in other browsers and platforms. Assume ASCII character set unless otherwise specified. This post is an excerpt from my [JavaScript RegExp](https://github.com/learnbyexample/learn_js_regexp) book.
13+
![regexp example]({{ '/images/books/pyregex_example.png' | absolute_url }}){: .align-center}
1414

15-
## Cheatsheet
15+
*Above diagram created using [Regulex](https://jex.im/regulex)*
16+
17+
<br>
18+
19+
This blog post gives an overview of regular expression syntax and features supported by JavaScript. Examples have been tested on Chrome/Chromium console (version 81+) and includes features not available in other browsers and platforms. Assume ASCII character set unless otherwise specified. This post is an excerpt from my [JavaScript RegExp](https://github.com/learnbyexample/learn_js_regexp) book.
20+
21+
## Elements that define a regular expression
1622

1723
| Note | Description |
1824
| ------- | ----------- |
@@ -37,8 +43,9 @@ This blog post gives an overview of regular expression syntax and features suppo
3743
| ------------- | ----------- |
3844
| `^` | restricts the match to start of string |
3945
| `$` | restricts the match to end of string |
40-
| `\n` | line separator |
4146
| `m` | flag to match the start/end of line with `^` and `$` anchors |
47+
| | `\r`, `\n`, `\u2028` and `\u2029` are line separators |
48+
| | dos-style files use `\r\n`, may need special attention |
4249
| `\b` | restricts the match to start/end of words |
4350
| | word characters: alphabets, digits, underscore |
4451
| `\B` | matches wherever `\b` doesn't match |
@@ -53,7 +60,7 @@ This blog post gives an overview of regular expression syntax and features suppo
5360
| `a(b|c)d` | same as `abd|acd` |
5461
| `(?:pat)` | non-capturing group |
5562
| `(?<name>pat)` | named capture group |
56-
| `.` | match any character except `\r` and `\n` characters |
63+
| `.` | match any character except line separators |
5764
| `[]` | Character class, matches one character among many |
5865

5966
| Greedy Quantifiers | Description |
@@ -125,7 +132,9 @@ This blog post gives an overview of regular expression syntax and features suppo
125132
| | use `\k<name>` for backreferencing in regexp definition |
126133
| | use `$<name>` for backreferencing in replacement section |
127134

128-
## Examples
135+
<br>
136+
137+
## Regular expression examples
129138

130139
* `test` method
131140

@@ -163,7 +172,7 @@ This blog post gives an overview of regular expression syntax and features suppo
163172
// string anchors
164173
> /^cat/.test('cater')
165174
< true
166-
> ['surrender', 'unicorn', 'newer', 'door'].filter(w => /er$/.test(w))
175+
> ['surrender', 'newer', 'door'].filter(w => /er$/.test(w))
167176
< ["surrender", "newer"]
168177

169178
// use 'm' flag to change string anchors to line anchors
@@ -210,7 +219,7 @@ This blog post gives an overview of regular expression syntax and features suppo
210219

211220
```js
212221
> function escapeRegExp(string) {
213-
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
222+
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&')
214223
}
215224

216225
> function unionRegExp(arr) {
@@ -227,15 +236,15 @@ This blog post gives an overview of regular expression syntax and features suppo
227236
// matches character '2', any character and then character '3'
228237
> '42\t33'.replace(/2.3/, '8')
229238
< "483"
230-
// 's' flag will allow newline character to be matched as well
239+
// 's' flag will allow line separators to be matched as well
231240
> 'Hi there\nHave a Nice Day'.replace(/the.*ice/s, 'X')
232241
< "Hi X Day"
233242

234243
// same as: /part|parrot|parent/g
235244
> 'par part parrot parent'.replace(/par(en|ro)?t/g, 'X')
236245
< "par X X X"
237246

238-
> ['abc', 'ac', 'abbc', 'xabbbcz', 'abbbbbc'].filter(w => /ab{1,4}c/.test(w))
247+
> ['abc', 'ac', 'abbc', 'xabbbcz'].filter(w => /ab{1,4}c/.test(w))
239248
< ["abc", "abbc", "xabbbcz"]
240249
```
241250

@@ -252,7 +261,8 @@ This blog post gives an overview of regular expression syntax and features suppo
252261
> 'cat and dog'.match(/dog/).index
253262
< 8
254263

255-
// get all matching portions with 'g' flag, no properties or group portions
264+
// get all matching portions with 'g' flag
265+
// no properties or group portions
256266
> 'par spar apparent spare part'.match(/\bs?par[et]\b/g)
257267
< ["spare", "part"]
258268

@@ -272,7 +282,8 @@ This blog post gives an overview of regular expression syntax and features suppo
272282
< [0, 4, 11]
273283

274284
// get only capture group portions as an array for each match
275-
> Array.from('xx:yyy x: x:yy :y'.matchAll(/(x*):(y*)/g), m => m.slice(1))
285+
> let s = 'xx:yyy x: x:yy :y'
286+
> Array.from(s.matchAll(/(x*):(y*)/g), m => m.slice(1))
276287
< (4) [Array(2), Array(2), Array(2), Array(2)]
277288
0: (2) ["xx", "yyy"]
278289
1: (2) ["x", ""]
@@ -305,7 +316,7 @@ This blog post gives an overview of regular expression syntax and features suppo
305316
// split based on one or more digit characters
306317
> 'Sample123string42with777numbers'.split(/\d+/)
307318
< ["Sample", "string", "with", "numbers"]
308-
// use capture group to include the portion that caused the split as well
319+
// include the portion that caused the split as well
309320
> 'Sample123string42with777numbers'.split(/(\d+)/)
310321
< ["Sample", "123", "string", "42", "with", "777", "numbers"]
311322

@@ -321,7 +332,8 @@ This blog post gives an overview of regular expression syntax and features suppo
321332
* backreferencing with normal/non-capturing/named capture groups
322333

323334
```js
324-
// remove any number of consecutive duplicate words separated by space
335+
// remove consecutive duplicate words separated by space
336+
// use \W+ instead of space to cover cases like 'a;a<-;a'
325337
> 'aa a a a 42 f_1 f_1 f_13.14'.replace(/\b(\w+)( \1)+\b/g, '$1')
326338
< "aa a 42 f_1 f_13.14"
327339

@@ -347,7 +359,7 @@ This blog post gives an overview of regular expression syntax and features suppo
347359
```js
348360
// change 'foo' only if it is not followed by a digit character
349361
// note that end of string satisfies the given assertion
350-
// 'foofoo' has two matches as the assertion doesn't consume characters
362+
// note that 'foofoo' has two matches
351363
> 'hey food! foo42 foot5 foofoo'.replace(/foo(?!\d)/g, 'baz')
352364
< "hey bazd! foo42 bazt5 bazbaz"
353365

@@ -360,7 +372,7 @@ This blog post gives an overview of regular expression syntax and features suppo
360372
< ["5", "20"]
361373

362374
// words containing all vowels in any order
363-
> let words = ['sequoia', 'subtle', 'questionable', 'exhibit', 'equation']
375+
> let words = ['sequoia', 'questionable', 'exhibit', 'equation']
364376
> words.filter(w => /(?=.*a)(?=.*e)(?=.*i)(?=.*o).*u/.test(w))
365377
< ["sequoia", "questionable", "equation"]
366378

0 commit comments

Comments
 (0)