Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 946a981

Browse files
updates for version 2.0
1 parent 6bc20cb commit 946a981

21 files changed

+1958
-1641
lines changed

‎Exercise_solutions.md

Lines changed: 367 additions & 294 deletions
Large diffs are not rendered by default.

‎Exercises.md

Lines changed: 317 additions & 272 deletions
Large diffs are not rendered by default.

‎LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Sundeep Agarwal
3+
Copyright (c) 2023 Sundeep Agarwal
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

‎README.md

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# JavaScript RegExp
1+
# Understanding JavaScript RegExp
22

3-
Example based guide to mastering JavaScript regexp.
3+
Learn JavaScript Regular Expressions step-by-step from beginner to advanced levels with hundreds of examples and exercises. Visit https://youtu.be/8X-hUel3GxM for a short video about the book.
44

5-
<p align="center">
6-
<img src="./images/js_regexp.png" width="320px" height="400px" />
7-
</p>
5+
<p align="center"><img src="./images/js_regexp_ls.png" alt="Understanding JavaScript RegExp ebook cover image" /></p>
86

9-
The book also includes exercises to test your understanding, which is presented together as a single file in this repo - [Exercises.md](./Exercises.md)
7+
The book also includes exercises to test your understanding, which are presented together as a single file in this repo [Exercises.md](./Exercises.md).
108

119
For solutions to the exercises, see [Exercise_solutions.md](./Exercise_solutions.md).
1210

@@ -16,24 +14,20 @@ See [Version_changes.md](./Version_changes.md) to keep track of changes made to
1614

1715
# E-book
1816

19-
You can purchase the pdf/epub versions of the book using these links:
17+
* You can purchase the pdf/epub versions of the book using these links:
18+
* https://learnbyexample.gumroad.com/l/js_regexp
19+
* https://leanpub.com/js_regexp
20+
* You can also get the book as part of these bundles:
21+
* **All books bundle** bundle from https://learnbyexample.gumroad.com/l/all-books
22+
* Includes all my programming books
23+
* **Awesome Regex** bundle from https://learnbyexample.gumroad.com/l/regex or https://leanpub.com/b/regex
24+
* See https://learnbyexample.github.io/books/ for a list of other books
2025

21-
* https://learnbyexample.gumroad.com/l/js_regexp
22-
* https://leanpub.com/js_regexp
26+
For a preview of the book, see [sample chapters](./sample_chapters/js_regexp_sample.pdf).
2327

24-
You can also get the book as part of these bundles:
28+
The book can also be [viewed as a single markdown file in this repo](./js_regexp.md). See my blogpost on [generating pdfs from markdown using pandoc](https://learnbyexample.github.io/customizing-pandoc/) if you are interested in the ebook creation process.
2529

26-
* **Awesome Regex** bundle from https://learnbyexample.gumroad.com/l/regex or https://leanpub.com/b/regex
27-
* **All books bundle** bundle from https://learnbyexample.gumroad.com/l/all-books
28-
* Includes all my programming books
29-
30-
See https://learnbyexample.github.io/books/ for list of other books
31-
32-
For a preview of the book, see [sample chapters](https://github.com/learnbyexample/learn_js_regexp/blob/master/sample_chapters/js_regexp_sample.pdf)
33-
34-
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.
35-
36-
For web version of the book, visit https://learnbyexample.github.io/learn_js_regexp/
30+
For the web version of the book, visit https://learnbyexample.github.io/learn_js_regexp/
3731

3832
<br>
3933

@@ -47,20 +41,22 @@ For web version of the book, visit https://learnbyexample.github.io/learn_js_reg
4741

4842
# Feedback
4943

50-
[Open an issue](https://github.com/learnbyexample/learn_js_regexp/issues) if you spot any typo/errors.
44+
⚠️ ⚠️ Please DO NOT submit pull requests. Main reason being any modification requires changes in multiple places.
5145

52-
:warning::warning: Please DO NOT submit pull requests. Main reason being any modification requires changes in multiple places.
46+
I would highly appreciate it if you'd let me know how you felt about this book. It could be anything from a simple thank you, pointing out a typo, mistakes in code snippets, which aspects of the book worked for you (or didn't!) and so on. Reader feedback is essential and especially so for self-published authors.
5347

54-
I'd also highly appreciate your feedback about the book.
48+
You can reach me via:
5549

56-
Twitter: https://twitter.com/learn_byexample
50+
* Issue Manager: [https://github.com/learnbyexample/learn_js_regexp/issues](https://github.com/learnbyexample/learn_js_regexp/issues)
51+
* E-mail: `echo 'bGVhcm5ieWV4YW1wbGUubmV0QGdtYWlsLmNvbQo=' | base64 --decode`
52+
* Twitter: [https://twitter.com/learn_byexample](https://twitter.com/learn_byexample)
5753

5854
<br>
5955

6056
# Table of Contents
6157

6258
1. Preface
63-
2. What's so special about Regular Expressions?
59+
2. Why is it needed?
6460
3. RegExp introduction
6561
4. Anchors
6662
5. Alternation and Grouping
@@ -79,13 +75,13 @@ Twitter: https://twitter.com/learn_byexample
7975

8076
# Acknowledgements
8177

82-
* [MDN: Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) — documentation and examples
83-
* [/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
78+
* [MDN: Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions) — documentation and examples
79+
* [/r/learnjavascript/](https://old.reddit.com/r/learnjavascript/) and [/r/regex/](https://old.reddit.com/r/regex/) — helpful forums for beginners and experienced programmers alike
8480
* [stackoverflow](https://stackoverflow.com/) — for getting answers to pertinent questions on JavaScript and regular expressions
8581
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on [pandoc](https://github.com/jgm/pandoc/) and `tex` related questions
86-
* Cover image: [LibreOffice Draw](https://www.libreoffice.org/discover/draw/) and [regulex](https://jex.im/regulex)
82+
* [canva](https://www.canva.com/) — cover image
8783
* [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
88-
* [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) for optimizing images
84+
* [oxipng](https://github.com/shssoichiro/oxipng), [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) optimizing images
8985
* [mdBook](https://github.com/rust-lang/mdBook) — for web version of the book
9086
* [mdBook-pagetoc](https://github.com/JorelAli/mdBook-pagetoc) — for adding table of contents for each chapter
9187
* [minify-html](https://github.com/wilsonzlin/minify-html) — for minifying html files
@@ -94,6 +90,7 @@ Twitter: https://twitter.com/learn_byexample
9490

9591
# License
9692

97-
The book is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/)
93+
The book is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/).
94+
95+
The code snippets are licensed under MIT, see [LICENSE](./LICENSE) file.
9896

99-
The code snippets are licensed under MIT, see [LICENSE](./LICENSE) file

‎Version_changes.md

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

3+
### 2.0
4+
5+
* Examples and exercises added for `d` and `v` flags
6+
* Strings in code snippets changed to be uniformly represented in single quotes
7+
* In general, many of the examples, exercises, solutions, descriptions and external links were updated/corrected
8+
* Updated Acknowledgements section
9+
* Code snippets related to info/warning sections will now appear as a single block
10+
* Book title changed to **Understanding JavaScript RegExp**
11+
* New cover image
12+
* Images centered for EPUB format
13+
14+
<br>
15+
316
### 1.6
417

518
* Code snippets checked to work with Chrome/Chromium console version 89+

‎code_snippets/Alternation_and_Grouping.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// OR conditional
1+
// Alternation
22

33
const pets = /cat|dog/
44

@@ -8,7 +8,7 @@ pets.test('I like dogs')
88

99
pets.test('I like parrots')
1010

11-
'catapults concatenate cat scat'.replace(/^cat|cat\b/g, 'X')
11+
'catapults concatenate cat scat cater'.replace(/^cat|cat\b/g, 'X')
1212

1313
'cat dog bee parrot fox'.replace(/cat|dog|fox/g, 'mammal')
1414

‎code_snippets/Anchors.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ words.filter(w => /t$/.test(w))
3636

3737
/er$/m.test('spare\npar\nera\ndare')
3838

39+
let elements = ['spare\ntool', 'par\n', 'dare', 'spared']
40+
41+
elements.filter(e => /are$/m.test(e))
42+
3943
/^par$/m.test('spare\npar\nera\ndare')
4044

4145
let items = 'catapults\nconcatenate\ncat'
@@ -44,39 +48,41 @@ console.log(items.replace(/^/gm, '* '))
4448

4549
console.log(items.replace(/$/gm, '.'))
4650

47-
'1\n2\n'.replace(/^/mg, 'foo ')
51+
console.log('1\n2\n'.replace(/^/mg, 'fig '))
4852

49-
'1\n2\n'.replace(/$/mg, ' baz')
53+
console.log('1\n2\n'.replace(/$/mg, ' apple'))
5054

5155
// Word anchors
5256

53-
let sample = 'par spar apparent spare part'
57+
let words = 'par spar apparent spare part'
58+
59+
words.replace(/par/g, 'X')
5460

55-
sample.replace(/par/g, 'X')
61+
words.replace(/\bpar/g, 'X')
5662

57-
sample.replace(/\bpar/g, 'X')
63+
words.replace(/par\b/g, 'X')
5864

59-
sample.replace(/par\b/g, 'X')
65+
words.replace(/\bpar\b/g, 'X')
6066

61-
sample.replace(/\bpar\b/g,'X')
67+
letwords='par spar apparent spare part'
6268

63-
letsample='par spar apparent spare part'
69+
console.log(words.replace(/\b/g,'"').replace(//g,','))
6470

65-
console.log(sample.replace(/\b/g, '"').replace(//g,','))
71+
'output=num1+35*42/num2'.replace(/\b/g, ' ')
6672

67-
'foo_baz=num1+35*42/num2'.replace(/\b/g, ' ')
73+
'output=num1+35*42/num2'.replace(/\b/g, ' ').trim()
6874

69-
'foo_baz=num1+35*42/num2'.replace(/\b/g,' ').trim()
75+
// Opposite Word Anchor
7076

71-
let sample = 'par spar apparent spare part'
77+
let words = 'par spar apparent spare part'
7278

73-
sample.replace(/\Bpar/g, 'X')
79+
words.replace(/\Bpar/g, 'X')
7480

75-
sample.replace(/\Bpar\b/g, 'X')
81+
words.replace(/\Bpar\b/g, 'X')
7682

77-
sample.replace(/par\B/g, 'X')
83+
words.replace(/par\B/g, 'X')
7884

79-
sample.replace(/\Bpar\B/g, 'X')
85+
words.replace(/\Bpar\B/g, 'X')
8086

8187
'copper'.replace(/\b/g, ':')
8288

‎code_snippets/Character_class.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
// Custom character sets
22

3-
['cute', 'cat', 'cot', 'coat', 'cost'].filter(w => /c[ou]t/.test(w))
3+
['cute', 'cat', 'cot', 'coat', 'cost','scuttle'].filter(w => /c[ou]t/.test(w))
44

55
'meeting cute boat site foot'.replace(/[aeo]+t/g, 'X')
66

7-
'Sample123string42with777numbers'.match(/[0123456789]+/g)
8-
97
// Range of characters
108

119
'Sample123string42with777numbers'.match(/[0-9]+/g)
1210

13-
'coat Bin food tar12 best'.match(/\b[a-z0-9]+\b/g)
11+
'coat Bin food tar12 best Apple fig_42'.match(/\b[a-z0-9]+\b/g)
1412

15-
'coat tin food put stoop best'.match(/\b[p-z][a-z]*\b/g)
13+
'coat tin food put stoop best fig_42 Pet'.match(/\b[p-z][a-z]*\b/g)
1614

17-
'coat tin food put stoop best'.match(/\b[a-fp-t]+\b/g)
15+
'coat tin food put stoop best fig_42 Pet'.match(/\b[a-fp-t]+\b/g)
1816

1917
// Negating character sets
2018

2119
'Sample123string42with777numbers'.match(/[^0-9]+/g)
2220

23-
'foo=42; baz=123'.replace(/^[^=]+/, '')
24-
25-
'foo:123:bar:baz'.replace(/^([^:]+:){2}/, '')
21+
'apple:123:banana:cherry'.replace(/^([^:]+:){2}/, '')
2622

27-
'foo=42; baz=123'.replace(/=[^=]+$/, '')
23+
'apple=42; cherry=123'.replace(/=[^=]+$/, '')
2824

2925
let words = ['tryst', 'fun', 'glyph', 'pity', 'why']
3026

@@ -46,17 +42,17 @@ words.filter(w => !/[aeiou]/.test(w))
4642

4743
console.log('5ba\\babc2'.match(/[a\\b]+/)[0])
4844

49-
// Escape sequence character sets
45+
// Escape sequence sets
5046

5147
'Sample123string42with777numbers'.split(/\d+/)
5248

5349
'sea eat car rat eel tea'.match(/\b\w/g).join('')
5450

55-
'tea sea-pit sit-lean bean'.match(/[\w\s]+/g)
51+
'tea sea-Pit Sit;(lean_2\tbean_3)'.match(/[\w\s]+/g)
5652

5753
'Sample123string42with777numbers'.replace(/\D+/g, '-')
5854

59-
' 1..3 \v\f foo_baz 42\tzzz \r\n1-2-3 '.match(/\S+/g)
55+
' 1..3 \v\f fig_tea 42\tzzz \r\n1-2-3 '.match(/\S+/g)
6056

6157
// Numeric ranges
6258

‎code_snippets/Dot_metacharacter_and_Quantifiers.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44

55
'breadth markedly reported overrides'.replace(/r..d/g, 'X')
66

7-
'42\t33'.replace(/2.3/, '8')
7+
'42\t35'.replace(/2.3/, '8')
88

9-
// split method
9+
'cag̈ed'.replace(/a.e/, 'o')
10+
11+
'cag̈ed'.replace(/a..e/, 'o')
12+
13+
// split() method
1014

1115
'apple-85-mango-70'.split(/-/)
1216

1317
'apple-85-mango-70'.split(/-/, 2)
1418

15-
'bus:3:car:5:van'.split(/:.:/)
19+
'bus:3:car:-:van'.split(/:.:/)
1620

1721
// Greedy quantifiers
1822

@@ -44,15 +48,19 @@
4448

4549
'3111111111125111142'.split(/1+/)
4650

47-
let demo = ['abc', 'ac', 'abbc', 'xabbbcz', 'bc', 'abbbbbc']
51+
let repeats = ['abc', 'ac', 'abbc', 'xabbbcz', 'bc', 'abbbbbc']
52+
53+
repeats.filter(w => /ab{1,4}c/.test(w))
4854

49-
demo.filter(w => /ab{1,4}c/.test(w))
55+
repeats.filter(w => /ab{0,2}c/.test(w))
5056

51-
demo.filter(w => /ab{0,2}c/.test(w))
57+
repeats.filter(w => /ab{3,}c/.test(w))
5258

53-
demo.filter(w => /ab{3,}c/.test(w))
59+
repeats.filter(w => /ab{3}c/.test(w))
5460

55-
demo.filter(w => /ab{3}c/.test(w))
61+
'a{5} = 10'.replace(/a\{5}/g, 'a{6}')
62+
63+
'report_{a,b}.txt'.replace(/_{a,b}/g, '-{c,d}')
5664

5765
// AND Conditional
5866

@@ -74,7 +82,7 @@ patterns.every(p => p.test('dog and cat'))
7482

7583
'foot'.replace(/f.?o/, 'X')
7684

77-
console.log('blah < foo \\< bar < baz'.replace(/\\?</g, '\\<'))
85+
console.log('table < fig \\< bat < cake'.replace(/\\?</g, '\\<'))
7886

7987
'hand handy handful'.replace(/hand(y|ful)?/g, 'X')
8088

@@ -96,19 +104,13 @@ sentence.replace(/t.*a.*u/, 'X')
96104

97105
'123456789'.replace(/.{2,5}?/, 'X')
98106

99-
'green:3.14:teal::brown:oh!:blue'.split(/:.*?:/)
100-
101-
let sentence = 'that is quite a fabricated tale'
102-
103-
sentence.replace(/t.*?a/, 'X')
107+
'green:3.14:teal::brown:oh!:blue'.split(/:.*:/)
104108

105-
'star'.replace(/t.*?a/, 'X')
106-
107-
sentence.replace(/t.*?a.*?f/, 'X')
109+
'green:3.14:teal::brown:oh!:blue'.split(/:.*?:/)
108110

109111
// s flag
110112

111-
'Hi there\nHave a Nice Day'.replace(/the.*ice/, 'X')
113+
console.log('Hi there\nHave a Nice Day'.replace(/the.*ice/, 'X'))
112114

113-
'Hi there\nHave a Nice Day'.replace(/the.*ice/s, 'X')
115+
console.log('Hi there\nHave a Nice Day'.replace(/the.*ice/s, 'X'))
114116

‎code_snippets/Escaping_metacharacters.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Escaping with \
1+
// Escaping with backslash
22

33
/b^2/.test('a^2 + b^2 - C*3')
44

@@ -64,13 +64,13 @@ p4.flags
6464

6565
'cater cat concatenate par spare'.replace(p4, 'X')
6666

67-
// Escaping delimiter
67+
// Escaping the delimiter
6868

69-
let path = '/abc/123/foo/baz/ip.txt'
69+
let path = '/home/joe/report/sales/ip.txt'
7070

71-
path.replace(/^\/abc\/123\//, '~/')
71+
path.replace(/^\/home\/joe\//, '~/')
7272

73-
path.replace(new RegExp(`^/abc/123/`), '~/')
73+
path.replace(new RegExp(`^/home/joe/`), '~/')
7474

7575
// Escape sequences
7676

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /