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: _posts/2019-12-06-javascript-regexp-cheatsheet.md
+27-15
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,15 @@ tags:
10
10
date: 2019-12-06T14:37:42
11
11
---
12
12
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.
*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
16
22
17
23
| Note | Description |
18
24
| ------- | ----------- |
@@ -37,8 +43,9 @@ This blog post gives an overview of regular expression syntax and features suppo
37
43
| ------------- | ----------- |
38
44
|`^`| restricts the match to start of string |
39
45
|`$`| restricts the match to end of string |
40
-
|`\n`| line separator |
41
46
|`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 |
42
49
|`\b`| restricts the match to start/end of words |
43
50
|| word characters: alphabets, digits, underscore |
44
51
|`\B`| matches wherever `\b` doesn't match |
@@ -53,7 +60,7 @@ This blog post gives an overview of regular expression syntax and features suppo
53
60
| `a(b|c)d` | same as `abd|acd` |
54
61
|`(?:pat)`| non-capturing group |
55
62
|`(?<name>pat)`| named capture group |
56
-
|`.`| match any character except `\r` and `\n` characters|
63
+
|`.`| match any character except line separators|
57
64
|`[]`| Character class, matches one character among many |
58
65
59
66
| Greedy Quantifiers | Description |
@@ -125,7 +132,9 @@ This blog post gives an overview of regular expression syntax and features suppo
125
132
|| use `\k<name>` for backreferencing in regexp definition |
126
133
|| use `$<name>` for backreferencing in replacement section |
127
134
128
-
## Examples
135
+
<br>
136
+
137
+
## Regular expression examples
129
138
130
139
*`test` method
131
140
@@ -163,7 +172,7 @@ This blog post gives an overview of regular expression syntax and features suppo
0 commit comments