Skip to content

Commit 83a82dc

Browse files
updates for version 1.6
1 parent 3540bdc commit 83a82dc

File tree

6 files changed

+32
-28
lines changed

6 files changed

+32
-28
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ You can purchase the book using these links:
2020

2121
* https://gumroad.com/l/js_regexp
2222
* https://leanpub.com/js_regexp
23-
* You can also get the book as part of **Awesome Regex** bundle from https://leanpub.com/b/regex or https://gumroad.com/l/regex
23+
* You can also get the book as part of **Awesome Regex** bundle from https://gumroad.com/l/regex or https://leanpub.com/b/regex
2424
* See https://learnbyexample.github.io/books/ for list of other books
2525

26-
For a preview of the book, see [sample chapters](https://github.com/learnbyexample/learn_js_regexp/blob/master/sample_chapters/js_regexp_sample_chapters.pdf)
26+
For a preview of the book, see [sample chapters](https://github.com/learnbyexample/learn_js_regexp/blob/master/sample_chapters/js_regexp_sample.pdf)
2727

2828
The book can also be [viewed as a single markdown file in this repo](./js_regexp.md). See my blogpost on [generating pdf from markdown using pandoc](https://learnbyexample.github.io/tutorial/ebook-generation/customizing-pandoc/) if you are interested in the ebook creation process.
2929

@@ -33,11 +33,11 @@ For web version of the book, visit https://learnbyexample.github.io/learn_js_reg
3333

3434
# Feedback
3535

36-
Please open an issue if you spot any typo/errors.
36+
[Open an issue](https://github.com/learnbyexample/learn_js_regexp/issues) if you spot any typo/errors.
3737

38-
I'd also highly appreciate your feedback about the book.
38+
:warning: :warning: Please DO NOT submit pull requests. Main reason being any modification requires changes in multiple places.
3939

40-
Goodreads: https://www.goodreads.com/book/show/49090622-javascript-regexp
40+
I'd also highly appreciate your feedback about the book.
4141

4242
Twitter: https://twitter.com/learn_byexample
4343

@@ -68,9 +68,10 @@ Twitter: https://twitter.com/learn_byexample
6868
* [MDN: Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) — documentation and examples
6969
* [/r/learnjavascript/](https://www.reddit.com/r/learnjavascript/) and [/r/regex/](https://www.reddit.com/r/regex/) — helpful forums for beginners and experienced programmers alike
7070
* [stackoverflow](https://stackoverflow.com/) — for getting answers to pertinent questions on JavaScript and regular expressions
71-
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on `pandoc` and `tex` related questions
71+
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on [pandoc](https://github.com/jgm/pandoc/) and `tex` related questions
7272
* Cover image: [LibreOffice Draw](https://www.libreoffice.org/discover/draw/) and [regulex](https://jex.im/regulex)
7373
* [Warning](https://commons.wikimedia.org/wiki/File:Warning_icon.svg) and [Info](https://commons.wikimedia.org/wiki/File:Info_icon_002.svg) icons by [Amada44](https://commons.wikimedia.org/wiki/User:Amada44) under public domain
74+
* [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) for optimizing images
7475
* [mdBook](https://github.com/rust-lang/mdBook) — for web version of the book
7576
* [mdBook-pagetoc](https://github.com/JorelAli/mdBook-pagetoc) — for adding table of contents for each chapter
7677
* [minify-html](https://github.com/wilsonzlin/minify-html) — for minifying html files

Version_changes.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<br>
22

3+
### 1.6
4+
5+
* Code snippets checked to work with Chrome/Chromium console version 89+
6+
* Updated `escapeRegExp` function as per [MDN: Regular Expressions doc](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
7+
* Typo and miscellaneous changes
8+
9+
<br>
10+
311
### 1.5
412

513
* Added **epub** version of the book

code_snippets/Escaping_metacharacters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// Dynamically escaping metacharacters
1212

1313
function escapeRegExp(string) {
14-
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&')
14+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
1515
}
1616

1717
let eqn = 'f*(a^b) - 3*(a^b)'

js_regexp.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,28 @@ A good understanding of basic-level programming concepts and prior experience wo
1010

1111
## Conventions
1212

13-
* The examples presented here have been tested on Chrome/Chromium console (version 81+) and includes features not available in other browsers and platforms.
13+
* The examples presented here have been tested on Chrome/Chromium console (version 89+) and includes features not available in other browsers and platforms.
1414
* Code snippets shown are copy pasted from the console and modified for presentation purposes. Some of the commands are preceded by comments to provide context and explanations. Blank lines have been added to improve readability and output is skipped when it is `undefined` or otherwise unnecessary to be shown.
1515
* Unless otherwise noted, all examples and explanations are meant for *ASCII* characters.
1616
* External links are provided for further reading throughout the book. Not necessary to immediately visit them. They have been chosen with care and would help, especially during rereads.
17-
* The [learn_js_regexp](https://github.com/learnbyexample/learn_js_regexp) repo has all the code snippets and exercises and other details related to the book. Click the **Clone** button to get the files.
17+
* The [learn_js_regexp](https://github.com/learnbyexample/learn_js_regexp) repo has all the code snippets, exercises and other details related to the book. Click the **Code** button to get the files.
1818

1919
## Acknowledgements
2020

2121
* [MDN: Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) — documentation and examples
2222
* [/r/learnjavascript/](https://www.reddit.com/r/learnjavascript/) and [/r/regex/](https://www.reddit.com/r/regex/) — helpful forums for beginners and experienced programmers alike
2323
* [stackoverflow](https://stackoverflow.com/) — for getting answers to pertinent questions on JavaScript and regular expressions
24-
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on `pandoc` and `tex` related questions
24+
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on [pandoc](https://github.com/jgm/pandoc/) and `tex` related questions
2525
* Cover image: [LibreOffice Draw](https://www.libreoffice.org/discover/draw/) and [regulex](https://jex.im/regulex)
2626
* [Warning](https://commons.wikimedia.org/wiki/File:Warning_icon.svg) and [Info](https://commons.wikimedia.org/wiki/File:Info_icon_002.svg) icons by [Amada44](https://commons.wikimedia.org/wiki/User:Amada44) under public domain
27+
* [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) for optimizing images
2728

2829
## Feedback and Errata
2930

3031
I would highly appreciate if you'd let me know how you felt about this book, it would help to improve this book as well as my future attempts. Also, please do let me know if you spot any error or typo.
3132

3233
Issue Manager: [https://github.com/learnbyexample/learn_js_regexp/issues](https://github.com/learnbyexample/learn_js_regexp/issues)
3334

34-
Goodreads: https://www.goodreads.com/book/show/49090622-javascript-regexp
35-
3635
3736

3837
Twitter: https://twitter.com/learn_byexample
@@ -53,7 +52,7 @@ Resources mentioned in Acknowledgements section above are available under origin
5352

5453
## Book version
5554

56-
1.5
55+
1.6
5756

5857
See [Version_changes.md](https://github.com/learnbyexample/learn_js_regexp/blob/master/Version_changes.md) to track changes across book versions.
5958

@@ -130,7 +129,7 @@ First up, a simple example to test whether a string is part of another string or
130129
> sentence.includes('z')
131130
< false
132131

133-
// check if 'sentence' matches the pattern as described by the regexp
132+
// check if 'sentence' matches the pattern as described by the RegExp object
134133
> /is/.test(sentence)
135134
< true
136135
> /z/.test(sentence)
@@ -194,7 +193,7 @@ Examples for `i` flag is shown below. `g` flag will be discussed in [replace met
194193

195194
## RegExp constructor and reuse
196195

197-
The RegExp object can be saved in a variable. This helps to improve code clarity, enable reuse, etc.
196+
The RegExp object can be saved in a variable. This helps to improve code clarity, enables reuse, etc.
198197

199198
```js
200199
> const pet = /dog/
@@ -225,7 +224,6 @@ The main advantage of the constructor over `//` literal is the ability to dynami
225224
> const pat1 = new RegExp(`${greeting} there`)
226225
> pat1
227226
< /hi there/
228-
229227
> new RegExp(`${greeting.toUpperCase()} there`)
230228
< /HI there/
231229
```
@@ -397,7 +395,7 @@ water 10`
397395

398396
# Anchors
399397

400-
In this chapter, you'll be learning about qualifying a pattern. Instead of matching anywhere in the given input string, restrictions can be specified. For now, you'll see the ones that are already part of regular expression features. In later chapters, you'll learn how to define your own rules for restriction.
398+
In this chapter, you'll be learning about qualifying a pattern. Instead of matching anywhere in the given string, restrictions can be specified. For now, you'll see the ones that are already part of regexp features. In later chapters, you'll learn how to define your own rules for restriction.
401399

402400
These restrictions are made possible by assigning special meaning to certain characters and escape sequences. The characters with special meaning are known as **metacharacters** in regexp parlance. In case you need to match those characters literally, you need to escape them with a `\` character (discussed in [Escaping metacharacters](#escaping-metacharacters) chapter).
403401

@@ -730,7 +728,6 @@ In a conditional expression, you can use the logical operators to combine multip
730728
// replace either 'cat' at start of string or 'cat' at end of word
731729
> 'catapults concatenate cat scat'.replace(/^cat|cat\b/g, 'X')
732730
< "Xapults concatenate X sX"
733-
734731
// replace either 'cat' or 'dog' or 'fox' with 'mammal'
735732
> 'cat dog bee parrot fox'.replace(/cat|dog|fox/g, 'mammal')
736733
< "mammal mammal bee parrot mammal"
@@ -900,7 +897,7 @@ When you are defining the regexp yourself, you can manually escape the metachara
900897

901898
```js
902899
> function escapeRegExp(string) {
903-
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&')
900+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
904901
}
905902
```
906903

@@ -925,7 +922,7 @@ There are many things in the above regexp that you haven't learnt yet. They'll b
925922
"f*(a^b) - 3*c"
926923
```
927924

928-
>![info](images/info.svg) Note that the `/` delimiter character isn't escaped in the above function. Use `[.*+\-?^${}()|[\]\\\/]` to escape the delimiter as well.
925+
>![info](images/info.svg) Note that the `/` delimiter character isn't escaped in the above function. Use `[.*+?^${}()|[\]\\\/]` to escape the delimiter as well.
929926
930927
## Dynamically building alternation
931928

@@ -1286,7 +1283,6 @@ Next up, how to construct AND conditional using dot metacharacter and quantifier
12861283
// match 'Error' followed by zero or more characters followed by 'valid'
12871284
> /Error.*valid/.test('Error: not a valid input')
12881285
< true
1289-
12901286
> /Error.*valid/.test('Error: key not found')
12911287
< false
12921288
```
@@ -2170,7 +2166,6 @@ As highlighted earlier, handle negative logic with care, as you might end up mat
21702166
// elements not containing vowel characters
21712167
> words.filter(w => /^[^aeiou]+$/.test(w))
21722168
< ["tryst", "glyph", "why"]
2173-
21742169
// easier to write and maintain, note the use of '!' operator
21752170
// but this'll match empty strings too unlike the previous solution
21762171
> words.filter(w => !/[aeiou]/.test(w))
@@ -2610,6 +2605,7 @@ When backreferencing is not required, you can use a non-capturing group to avoid
26102605
// but using capture group will not give expected output
26112606
> '123hand42handy777handful500'.split(/hand(y|ful)?/)
26122607
< ["123", undefined, "42", "y", "777", "ful", "500"]
2608+
26132609
// non-capturing group to the rescue
26142610
> '123hand42handy777handful500'.split(/hand(?:y|ful)?/)
26152611
< ["123", "42", "777", "500"]
@@ -2877,7 +2873,7 @@ Make sure to test these patterns for your use case. For example, the below data
28772873

28782874
## Summary
28792875

2880-
Some patterns are quite complex and not easy to build and validate from scratch. Libraries like **CommonRegexJS** are helpful to reduce your time and effort needed for commonly known tasks. However, you do need to **test** the solution for *your* use case. See also [stackoverflow: validating email addresses](https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression).
2876+
Some patterns are quite complex and not easy to build and validate from scratch. Libraries like **CommonRegexJS** are helpful to reduce your time and effort needed for commonly known tasks. However, you do need to **test** the solution for *your* use case. See also [stackoverflow: validating email addresses](https://stackoverflow.com/q/201323/4082052).
28812877

28822878
# Lookarounds
28832879

@@ -2893,7 +2889,6 @@ Before you get used to lookarounds too much, it is good to remember that JavaScr
28932889
// filter elements containing digit and '#' characters
28942890
> items.filter(s => /\d/.test(s) && s.includes('#'))
28952891
< ["#foo 123"]
2896-
28972892
// modify elements only if it doesn't start with '#'
28982893
> items.filter(s => s[0] != '#').map(s => s.replace(/,.+,/, ' '))
28992894
< ["1 4", "a d"]
@@ -2964,7 +2959,7 @@ In all the examples so far, lookahead grouping was placed as a suffix and lookbe
29642959
< "foo_baz = num1 + 35 * 42 / num2"
29652960
```
29662961

2967-
>![info](images/info.svg) See [this stackoverflow Q&A](https://stackoverflow.com/questions/50011366/javascript-regex-negative-lookbehind-not-working-in-firefox) for a workaround if lookbehind isn't supported.
2962+
>![info](images/info.svg) See [this stackoverflow Q&A](https://stackoverflow.com/q/50011366/4082052) for a workaround if lookbehind isn't supported.
29682963
29692964
## Positive lookarounds
29702965

@@ -3386,7 +3381,7 @@ You can also use codepoints inside `\u{}` construct to specify unicode character
33863381
< ["fox", "eagle"]
33873382
```
33883383
3389-
>![info](images/info.svg) See also [stackoverflow: Unicode string to hex](https://stackoverflow.com/questions/21647928/javascript-unicode-string-to-hex).
3384+
>![info](images/info.svg) See also [stackoverflow: Unicode string to hex](https://stackoverflow.com/q/21647928/4082052).
33903385
33913386
## Cheatsheet and Summary
33923387
@@ -3447,7 +3442,7 @@ Note that some of these resources are not specific to JavaScript, so use them wi
34473442
* [Awesome Regex](https://github.com/aloisdg/awesome-regex) — curated collection of libraries, tools, frameworks and software
34483443
* [randexp.js](https://github.com/fent/randexp.js) — Generate strings that match a given regular expression
34493444
* [stackoverflow: JavaScript regexp](https://stackoverflow.com/questions/tagged/javascript+regex?sort=votes&pageSize=15)
3450-
* [stackoverflow: regex FAQ](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean)
3445+
* [stackoverflow: regex FAQ](https://stackoverflow.com/q/22937618/4082052)
34513446
* [stackoverflow: regex tag](https://stackoverflow.com/questions/tagged/regex) is a good source of exercise questions
34523447
* [rexegg](https://www.rexegg.com/) — tutorials, tricks and more
34533448
* [regular-expressions](https://www.regular-expressions.info/) — tutorials and tools
@@ -3460,6 +3455,6 @@ Here's some links for specific topics:
34603455
* [rexegg: best regex trick](https://www.rexegg.com/regex-best-trick.html)
34613456
* [regular-expressions: matching numeric ranges](https://www.regular-expressions.info/numericranges.html)
34623457
* [regular-expressions: Zero-Length Matches](https://www.regular-expressions.info/zerolength.html)
3463-
* [stackoverflow: Greedy vs Reluctant vs Possessive Quantifiers](https://stackoverflow.com/questions/5319840/greedy-vs-reluctant-vs-possessive-quantifiers)
3458+
* [stackoverflow: Greedy vs Reluctant vs Possessive Quantifiers](https://stackoverflow.com/q/5319840/4082052)
34643459
* [cloudflare: Details of the Cloudflare outage on July 2, 2019](https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/) — see appendix for details about CPU exhaustion caused due to regular expression backtracking
34653460

sample_chapters/js_regexp_sample.pdf

343 KB
Binary file not shown.
-337 KB
Binary file not shown.

0 commit comments

Comments
 (0)