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 0891699

Browse files
updates for version 1.5
1 parent f051dc5 commit 0891699

18 files changed

+3771
-508
lines changed

‎Exercise_solutions.md

Lines changed: 1359 additions & 0 deletions
Large diffs are not rendered by default.

‎Exercises.md

Lines changed: 872 additions & 165 deletions
Large diffs are not rendered by default.

‎code_snippets/Alternation_and_Grouping.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// OR conditional
2+
13
const pets = /cat|dog/
24

35
pets.test('I like cats')
@@ -10,6 +12,8 @@ pets.test('I like parrots')
1012

1113
'cat dog bee parrot fox'.replace(/cat|dog|fox/g, 'mammal')
1214

15+
// Grouping
16+
1317
'red reform read arrest'.replace(/reform|rest/g, 'X')
1418

1519
'red reform read arrest'.replace(/re(form|st)/g, 'X')
@@ -20,6 +24,8 @@ pets.test('I like parrots')
2024

2125
'par spare part party'.replace(/\bpar(|t)\b/g, 'X')
2226

27+
// Precedence rules
28+
2329
let words = 'lion elephant are rope not'
2430

2531
words.replace(/on|ant/, 'X')

‎code_snippets/Anchors.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// String anchors
2+
13
/^cat/.test('cater')
24

35
/^cat/.test('concatenation')
@@ -28,6 +30,8 @@ words.filter(w => /t$/.test(w))
2830

2931
'hack'.replace(/$/, 'er')
3032

33+
// Line anchors
34+
3135
/^top/m.test('hi hello\ntop spot')
3236

3337
/er$/m.test('spare\npar\nera\ndare')
@@ -44,6 +48,8 @@ console.log(items.replace(/$/gm, '.'))
4448

4549
'1\n2\n'.replace(/$/mg, ' baz')
4650

51+
// Word anchors
52+
4753
let sample = 'par spar apparent spare part'
4854

4955
sample.replace(/par/g, 'X')
@@ -54,12 +60,16 @@ sample.replace(/par\b/g, 'X')
5460

5561
sample.replace(/\bpar\b/g, 'X')
5662

63+
let sample = 'par spar apparent spare part'
64+
5765
console.log(sample.replace(/\b/g, '"').replace(//g, ','))
5866

5967
'foo_baz=num1+35*42/num2'.replace(/\b/g, ' ')
6068

6169
'foo_baz=num1+35*42/num2'.replace(/\b/g, ' ').trim()
6270

71+
let sample = 'par spar apparent spare part'
72+
6373
sample.replace(/\Bpar/g, 'X')
6474

6575
sample.replace(/\Bpar\b/g, 'X')

‎code_snippets/Character_class.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
// Custom character sets
2+
13
['cute', 'cat', 'cot', 'coat', 'cost'].filter(w => /c[ou]t/.test(w))
24

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

57
'Sample123string42with777numbers'.match(/[0123456789]+/g)
68

9+
// Range of characters
10+
711
'Sample123string42with777numbers'.match(/[0-9]+/g)
812

913
'coat Bin food tar12 best'.match(/\b[a-z0-9]+\b/g)
@@ -12,17 +16,7 @@
1216

1317
'coat tin food put stoop best'.match(/\b[a-fp-t]+\b/g)
1418

15-
'23 154 12 26 98234'.match(/\b[12][0-9]\b/g)
16-
17-
'23 154 12 26 98234'.match(/\b[0-9]{3,}\b/g)
18-
19-
'0501 035 154 12 26 98234'.match(/\b0*[1-9][0-9]{2,}\b/g)
20-
21-
'45 349 651 593 4 204'.match(/[0-9]+/g).filter(n => n < 350)
22-
23-
'45 349 651 593 4 204'.replace(/[0-9]+/g, m => m < 350 ? 0 : 1)
24-
25-
'45 349 651 593 4 204'.match(/[0-9]+/g).filter(n => n >= 200 && n <= 650)
19+
// Negating character sets
2620

2721
'Sample123string42with777numbers'.match(/[^0-9]+/g)
2822

@@ -38,6 +32,8 @@ words.filter(w => /^[^aeiou]+$/.test(w))
3832

3933
words.filter(w => !/[aeiou]/.test(w))
4034

35+
// Matching metacharacters literally
36+
4137
'ab-cd gh-c 12-423'.match(/\b[a-z-]{2,}\b/g)
4238

4339
'ab-cd gh-c 12-423'.match(/\b[a-z\-0-9]{2,}\b/g)
@@ -50,6 +46,8 @@ words.filter(w => !/[aeiou]/.test(w))
5046

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

49+
// Escape sequence character sets
50+
5351
'Sample123string42with777numbers'.split(/\d+/)
5452

5553
'sea eat car rat eel tea'.match(/\b\w/g).join('')
@@ -60,3 +58,17 @@ console.log('5ba\\babc2'.match(/[a\\b]+/)[0])
6058

6159
' 1..3 \v\f foo_baz 42\tzzz \r\n1-2-3 '.match(/\S+/g)
6260

61+
// Numeric ranges
62+
63+
'23 154 12 26 98234'.match(/\b[12]\d\b/g)
64+
65+
'23 154 12 26 98234'.match(/\b\d{3,}\b/g)
66+
67+
'0501 035 154 12 26 98234'.match(/\b0*[1-9]\d{2,}\b/g)
68+
69+
'45 349 651 593 4 204'.match(/\d+/g).filter(n => n < 350)
70+
71+
'45 349 651 593 4 204'.replace(/\d+/g, m => m < 350 ? 0 : 1)
72+
73+
'45 349 651 593 4 204'.match(/\d+/g).filter(n => n >= 200 && n <= 650)
74+

‎code_snippets/Dot_metacharacter_and_Quantifiers.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
'tac tin cat abc;tuv acute'.replace(/c.t/g, 'X')
1+
// Dot metacharacter
2+
3+
'tac tin c.t abc;tuv acute'.replace(/c.t/g, 'X')
24

35
'breadth markedly reported overrides'.replace(/r..d/g, 'X')
46

57
'42\t33'.replace(/2.3/, '8')
68

9+
// split method
10+
11+
'apple-85-mango-70'.split(/-/)
12+
13+
'apple-85-mango-70'.split(/-/, 2)
14+
15+
'bus:3:car:5:van'.split(/:.:/)
16+
17+
// Greedy quantifiers
18+
719
'far feat flare fear'.replace(/e?ar/g, 'X')
820

921
'par spare part party'.replace(/\bpart?\b/g, 'X')
@@ -42,6 +54,8 @@ demo.filter(w => /ab{3,}c/.test(w))
4254

4355
demo.filter(w => /ab{3}c/.test(w))
4456

57+
// AND Conditional
58+
4559
/Error.*valid/.test('Error: not a valid input')
4660

4761
/Error.*valid/.test('Error: key not found')
@@ -56,6 +70,8 @@ patterns.every(p => p.test('cat and dog'))
5670

5771
patterns.every(p => p.test('dog and cat'))
5872

73+
// What does greedy mean?
74+
5975
'foot'.replace(/f.?o/, 'X')
6076

6177
console.log('blah < foo \\< bar < baz'.replace(/\\?</g, '\\<'))
@@ -72,18 +88,26 @@ sentence.replace(/t.*a.*q.*f/, 'X')
7288

7389
sentence.replace(/t.*a.*u/, 'X')
7490

91+
// Non-greedy quantifiers
92+
7593
'foot'.replace(/f.??o/, 'X')
7694

7795
'frost'.replace(/f.??o/, 'X')
7896

7997
'123456789'.replace(/.{2,5}?/, 'X')
8098

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

83105
'star'.replace(/t.*?a/, 'X')
84106

85107
sentence.replace(/t.*?a.*?f/, 'X')
86108

109+
// s flag
110+
87111
'Hi there\nHave a Nice Day'.replace(/the.*ice/, 'X')
88112

89113
'Hi there\nHave a Nice Day'.replace(/the.*ice/s, 'X')

‎code_snippets/Escaping_metacharacters.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Escaping with \
2+
13
/b^2/.test('a^2 + b^2 - C*3')
24

35
/b\^2/.test('a^2 + b^2 - C*3')
@@ -6,8 +8,10 @@
68

79
'\\learn\\by\\example'.replace(/\\/g, '/')
810

11+
// Dynamically escaping metacharacters
12+
913
function escapeRegExp(string) {
10-
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
14+
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&')
1115
}
1216

1317
let eqn = 'f*(a^b) - 3*(a^b)'
@@ -22,6 +26,8 @@ eqn.replace(pat, 'c')
2226

2327
eqn.replace(new RegExp(escapeRegExp(usr_str) + '$'), 'c')
2428

29+
// Dynamically building alternation
30+
2531
function unionRegExp(arr) {
2632
return arr.map(w => escapeRegExp(w)).join('|')
2733
}
@@ -44,6 +50,8 @@ p2
4450

4551
'handful handed handy hands hand'.replace(p2, 'X')
4652

53+
// source and flags properties
54+
4755
const p3 = /\bpar\b/
4856

4957
const p4 = new RegExp(p3.source + '|cat', 'g')
@@ -56,9 +64,33 @@ p4.flags
5664

5765
'cater cat concatenate par spare'.replace(p4, 'X')
5866

59-
let path = '/foo/123/foo/baz/ip.txt'
67+
// Escaping delimiter
68+
69+
let path = '/abc/123/foo/baz/ip.txt'
70+
71+
path.replace(/^\/abc\/123\//, '~/')
72+
73+
path.replace(new RegExp(`^/abc/123/`), '~/')
74+
75+
// Escape sequences
76+
77+
'a\tb\tc'.replace(/\t/g, ':')
78+
79+
'1\n2\n3'.replace(/\n/g, ' ')
80+
81+
new RegExp('123\tabc')
82+
83+
new RegExp('123\\tabc')
84+
85+
new RegExp('car\b')
86+
87+
new RegExp('car\\b')
88+
89+
/\e/.test('hello')
90+
91+
'h e l l o'.replace(/\x20/g, '')
6092

61-
path.replace(/^\/foo\/123\//, '~/')
93+
'12|30'.replace(/2\x7c3/g, '5')
6294

63-
path.replace(newRegExp(`^/foo/123/`), '~/')
95+
'12|30'.replace(/2|3/g, '5')
6496

‎code_snippets/Groupings_and_backreferences.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
'[52] apples and [31] mangoes'.replace(/\[(\d+)\]/g, '1ドル')
1+
// Backreferences
2+
3+
'[52] apples [and] [31] mangoes'.replace(/\[(\d+)\]/g, '1ドル')
24

35
'_foo_ __123__ _baz_'.replace(/(_)?_/g, '1ドル')
46

@@ -16,12 +18,20 @@ words.filter(w => /(\w)1円/.test(w))
1618

1719
'aa a a a 42 f_1 f_1 f_13.14'.replace(/\b(\w+)(1円)+\b/g, '1ドル')
1820

21+
// Backreference oddities
22+
1923
'cat'.replace(/a/, '{1ドル}')
2024

2125
'cat'.replace(/(a)/, '{\1ドル}')
2226

2327
'cat'.replace(/(a)/, '{$1ドル}')
2428

29+
'[52] apples and [31] mangoes'.replace(/\[(\d+)\]/g, '(15ドル)')
30+
31+
'[52] apples and [31] mangoes'.replace(/\[(\d+)\]/g, '3ドル')
32+
33+
'[52] apples and [31] mangoes'.replace(/\[\d+\]/g, '1ドル')
34+
2535
'abcdefghijklmn'.replace(/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/, '11ドル')
2636

2737
'abcdefghijklmn'.replace(/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)/, '1ドル\x31')
@@ -30,12 +40,16 @@ words.filter(w => /(\w)1円/.test(w))
3040

3141
'abcdefghijklmna1d'.replace(/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.).*1円\x31/, 'X')
3242

43+
// Non-capturing groups
44+
3345
'Sample123string42with777numbers'.split(/\d+/)
3446

3547
'Sample123string42with777numbers'.split(/(\d+)/)
3648

3749
'effort flee facade oddball rat tool'.match(/\b\w*(\w)1円\w*\b/g)
3850

51+
'hi 123123123 bye 456123456'.match(/(123)+/g)
52+
3953
'123hand42handy777handful500'.split(/hand(y|ful)?/)
4054

4155
'123hand42handy777handful500'.split(/hand(?:y|ful)?/)
@@ -48,6 +62,8 @@ words.filter(w => /(\w)1円/.test(w))
4862

4963
'so:cat:rest:in:put:to'.replace(/^((?:[^:]+:){4})/, '(1ドル)')
5064

65+
// Named capture groups
66+
5167
let row = 'today,2008年03月24日,food,2008年03月24日,nice,2018年10月25日,5632'
5268

5369
row.match(/(?<date>\d{4}-\d{2}-\d{2}).*\k<date>/)[0]

‎code_snippets/Interlude_Common_tasks.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// CommonRegexJS
2+
3+
let data = 'hello 255.21.255.22 okay'
4+
5+
const comm = new CommonRegex(data)
6+
7+
comm.IPv4
8+
9+
let new_data = '23.14.2.4.2 255.21.255.22 567.12.2.1'
10+
11+
const ip = new CommonRegex(new_data)
12+
13+
ip.IPv4
14+

0 commit comments

Comments
(0)

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