-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathslides.html
928 lines (823 loc) · 33.9 KB
/
slides.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>/Reg(exp){2}lained/</title>
<link href="../csss/slideshow.css" rel="stylesheet" />
<link href="../csss/reusable.css" rel="stylesheet" />
<link href="../talks/shared/intro-outro.css" rel="stylesheet" />
<link href="css/global.css" rel="stylesheet" />
<link href="css/theme.css" rel="stylesheet" />
<link href="css/talk.css" rel="stylesheet" />
<link href="css/regex-test.css" rel="stylesheet" />
</head>
<body>
<header id="intro" class="slide dont-resize">
<hgroup>
<h1>/Reg(exp){2}lained/</h1>
<h2>Demystifying regular expressions</h2>
</hgroup>
<p class="attribution">By Lea Verou (@LeaVerou)</p>
</header>
<section id="introduction">
<section id="hi" class="inverse slide dont-resize darker hi">
<figure class="delayed">
<img src="../talks/shared/img/me-pirate.png" alt="Picture of me" title="Pirate illustration by Lorenzo Grandi http://www.lorenzograndi.it/" />
<figcaption style="font-size: 240%">Привет, я Лиа</figcaption>
</figure>
</section>
<section class="inverse slide dont-resize darker hi" id="lesbian" data-steps="2">
<div>
<p class="funfact">#funfact</p>
<p>I grew up in Lesbos, Greece</p>
<p>…which technically makes me <strong>geographically Lesbian</strong></p>
</div>
</section>
<section class="top-heavy slide dont-resize" id="projects">
<ul class="plain">
<li><a href="http://mavo.io/" style="background-image: url(../talks/shared/img/project-mavo.png)">Mavo</a></li>
<li><a href="http://prismjs.com" style="background-image: url(../talks/shared/img/project-prism.png)">PrismJS</a></li>
<li><a href="http://leaverou.github.io/prefixfree" style="background-image: url(../talks/shared/img/project-prefixfree.png)">-prefix-free</a></li>
<li><a href="http://blissfuljs.com" style="background-image: url(../talks/shared/img/project-bliss.png)">Bliss</a></li>
<li><a href="http://css3test.com" style="background-image: url(../talks/shared/img/project-css3test.png)">css3test.com</a></li>
<li><a href="http://leaverou.github.io/awesomplete/" style="background-image: url(../talks/shared/img/project-awesomplete.png)">Awesomplete</a></li>
<li><a href="http://cubic-bezier.com/" style="background-image: url(../talks/shared/img/project-cubicbezier.png)">cubic-bezier.com</a></li>
<li><a href="http://dabblet.com" style="background-image: url(../talks/shared/img/project-dabblet.png)">dabblet.com</a></li>
<li><a href="http://leaverou.github.io/contrast-ratio/" style="background-image: url(../talks/shared/img/project-contrastratio.png)">Contrast Ratio</a></li>
</ul>
</section>
<section class="top-heavy image slide" id="csswg" data-credits="Daniel Glazman" data-steps="1">
<h1 class="caption">
<!-- <img src="../talks/shared/img/w3c.svg" alt="W3C" class="inline"> -->
W3C CSS Working Group Invited Expert
</h1>
<svg viewBox="0 0 100 100" class="annotation">
<ellipse />
</svg>
</section>
<section class="inverse slide dont-resize darker hi" id="book">
<a href="http://shop.oreilly.com/product/0636920031123.do">
<img src="../talks/shared/img/book.png" alt="CSS Secrets by O’Reilly" style="display:block" />
★★★★★ on Amazon
</a>
</section>
<article class="image slide" style="background: url(img/russian-csssecrets.jpg) center/contain no-repeat white">
</article>
<section class="top-heavy image slide" id="mit">
<h1><strong>2014 –</strong> Haystack Group, MIT CSAIL</h1>
</section>
</section>
<section class="slide dont-resize" id="names">
<div class="speech-bubble">
<ul>
<li>“Regular expression”</li>
<li>“Regexp”</li>
<li>“Regex”</li>
<li class="delayed invalid">“ASCII puke”</li>
</ul>
</div>
<div class="delayed speech-bubble">
<p><pre>/^Reg(exp?|ular expression)$/i</pre></p>
</div>
</section>
<article class="image slide" style="background-image: url(img/scaredcat.jpg)" title="Fear"></article>
<section class="image slide" style="background-image: url(img/strings.jpg)" data-source="http://www.flickr.com/photos/gargi/3010324017/">
<dl class="caption">
<dt>Regular expressions:</dt>
<dd>A way to describe a set of strings</dd>
</dl>
</section>
<section class="slide syntax-breakdown">
<h1><code style="font-size: 7vw;">
<span title="Starts with a slash">/</span>
<span title="Pattern (what to match)" data-index="1">&(lt|gt|amp);</span>
<span title="Ends with a slash">/</span>
<span title="Flags (combination of g, i, m, y, u, s)" data-index="1">gi</span>
</code></h1>
</section>
<section class="slide syntax-breakdown">
<h1><code style="font-size: 5.8vw;">/
<span title="Beginning of string">^</span>
Reg(ex
<span title="Maybe one p" data-index="1">p?</span>
<span title="or" data-index="1">|</span>
ular expression)
<span title="End of string">$</span>
/
<span title="Case insensitive" data-index="1">i</span>
</code></h1>
</section>
<section class="narrow slide">
<pre><code class="language-javascript">"pâté".normalize("NFD").replace(/[\u0300-\u036f]/g, "") // "pate"</code></pre>
<pre><code class="language-javascript">'[email protected]'.match(/(\w+)@/)[1] // "lea"</code></pre>
<pre><code class="language-javascript">code.replace(/ {4}/g, '\t'); // fix broken indentation</code></pre>
<pre><code class="language-markup"><input name="zip" pattern="\d{5}" /> </code></pre>
<p>Also: text editors, IDEs, command line tools (grep, etc), databases, and more</p>
</section>
<section>
<!-- <section class="slide" id="methods">
<h1>RegExp aware methods</h1>
<h2><code>RegExp.prototype</code></h2>
<ul>
<li><code class="language-javascript">test(String)</code></li>
<li><code class="language-javascript">exec(String)</code></li>
</ul>
<h2 style="margin-top: 1em"><code>String.prototype</code></h2>
<ul>
<li><code class="language-javascript">match(RegExp)</code></li>
<li><code class="language-javascript">replace(RegExp, String | Function)</code></li>
<li><code class="language-javascript">split(RegExp)</code></li>
<li><code class="language-javascript">search(RegExp)</code></li>
</ul>
</section> -->
<section class="slide" data-src="index.html" data-title="Try stuff out at regexplained.com"></section>
<section class="regex-test slide" data-pattern="a" title="Literals">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>Regexes match anywhere, unless restricted</li>
<li>Matches cannot intersect</li>
<li>Case sensitive by default, use the <code>i</code> flag to change</li>
<li>Unicode characters by <code>\uXXXX</code></li>
</ul>
<p class="hint">\u2665 for ♥, emojis, rainbow matches rainbow flag emoji</p>
</section>
<article class="slide" title="Flags">
<table>
<thead>
<th>Flag</th>
<th>Name</th>
<th>Purpose</th>
<th>ES?</th>
</thead>
<tr>
<td><code>g</code></td>
<td>Global</td>
<td>Get all matches</td>
<td>ES3</td>
</tr>
<tr>
<td><code>i</code></td>
<td>Case Insensitive</td>
<td>Ignore case when matching</td>
<td>ES3</td>
</tr>
<tr>
<td><code>m</code></td>
<td>Multiline</td>
<td><code>^</code> and <code>$</code> match the beginning and end of each line, not of the whole string</td>
<td>ES3</td>
</tr>
<tr>
<td><code>y</code></td>
<td>Sticky</td>
<td>Anchors each match of a regular expression to the end of the previous match.</td>
<td>ES2015</td>
</tr>
<tr>
<td><code>u</code></td>
<td>Unicode</td>
<td>Treat pattern as a sequence of unicode code points</td>
<td>ES2015</td>
</tr>
<tr>
<td><code>s</code></td>
<td>DotAll</td>
<td><code>.</code> matches newlines as well.</td>
<td>ES2018? (Stage 3)</td>
</tr>
</table>
</article>
<section class="regex-test slide" data-pattern="cat|bat|dog" data-test="cat" title="Grouping">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>| for alternatives</li>
<li>Group with parenthses (Useful for alternatives or quantifiers)</li>
<li>Alternatives can be empty</li>
<li>Order matters!</li>
</ul>
<p class="hint">(c|b|f|)at|dog, do(|g) for order</p>
</section>
<section class="slide" data-type="Challenge" data-duration="1" data-video="kitten.mp4" style="--position: right">
<h1>Test Challenge</h1>
<p>
<code>fizz</code>
<code>buzz</code>
<code>fizzbuzz</code>
</p>
</section>
<article class="narrow slide language-javascript" data-type="Solution">
<h1>Fizzbuzz</h1>
<pre><code>// Matches everything, including ""
/(fizz|)(buzz|)/</code></pre>
<pre class="delayed persistent"><code>// Correct but inelegant
/(fizzbuzz|fizz|buzz)/</code></pre>
<pre class="delayed persistent"><code>/(fizz(buzz|)|buzz)/</code></pre>
</article>
<!-- <section class="slide dont-resize" data-type="Challenge" data-duration="1" data-video="hamsters.webm" style="--position: right">
<h1>Font & Image MIME types</h1>
<p>
<code>image/png</code>
<code>image/jpeg</code>
<code>image/gif</code>
<code>font/ttf</code>
<code>font/otf</code>
<code>font/woff</code>
<code>font/woff2</code>
<span class="not">not</span> <code>image/ttf</code> <code>font/gif</code> etc
</p>
</section>
<section class="narrow slide language-javascript" data-type="Solution">
<h1 style="white-space: nowrap;">Font & Image MIME types</h1>
<pre><code>// Wrong! Matches image/ttf, font/gif etc
/(image|font)\/(png|jpeg|gif|ttf|otf|woff|woff2)/</code></pre>
<pre class="delayed persistent"><code>// Correct, but could be shorter
/image\/(png|jpeg|gif)|font\/ttf|otf|woff|woff2)/</code></pre>
<pre class="delayed persistent"><code>/image\/(png|jpeg|gif)|font\/(ttf|otf|woff(2|))/</code></pre>
<pre class="delayed persistent"><code>// Please don’t
/image\/(png|jpeg|gif)|font\/(t|o)tf|woff(|2))/</code></pre>
</section> -->
<section class="slide" data-type="Challenge" data-duration="2" data-video="hamsters.webm">
<h1>Emojis with woman</h1>
<p style="font-size: 200%;">
<code>👩</code>
<code>👩💼</code>
<code>👪</code>
<code>👨👩👦👦</code>
<code>👩👦👦</code>
<code>👩👧</code>
<code>👩👦</code>
<code>👩👩👦👦</code>
<code>👩👩👦</code>
<code>👩👩👧</code>
<code>💏</code>
<code>👩❤️💋👩</code>
<strong class="not">not</strong>
<code>👨👧</code>
<code>👨👨👧</code>
<code>👨👨👦</code>
<code>👨❤️👨</code>
</p>
</section>
<section class="narrow slide" data-type="Solution">
<h1>Emojis with woman</h1>
<pre><code class="language-javascript">// Ewww, just stop
/👩|👩💼|👪|👩👦👦|👩👧|👩👦|👩👩👦👦|👩👩👦|👩👩👧|👨👩👦👦|.../</code></pre>
<pre class="delayed persistent"><code class="language-javascript">/👩/ // Wrong, doesn’t include 👪 and 💏!</code></pre>
<pre class="delayed persistent"><code class="language-javascript">/👩|👪|💏/</code></pre>
</section>
<section class="regex-test slide" data-pattern="1.5" data-test="1.5" title="The dot metacharacter">
<p class="hint">/1.5/ show it matches 21056 etc, show two are needed to match emojis</p>
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>Dot (.) matches anything, except line breaks</li>
<li>The experimental <code>s</code> flag makes it match line breaks</li>
<li>Escape metacharacters with a backslash</li>
</ul>
</section>
<section class="slide dont-resize" id="metacharacters">
<h1>The 12 metacharacters</h1>
<code>[</code>
<code>{</code>
<code>(</code>
<code>)</code>
<code>\</code>
<code>^</code>
<code>$</code>
<code>.</code>
<code>|</code>
<code>?</code>
<code>*</code>
<code>+</code>
</section>
<section class="regex-test slide" data-pattern="aaaaargh" title="Quantifiers">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>{n} = n times</li>
<li>{m,n} = at least m times but no more than n times</li>
<li>{m,} = at least m times</li>
</ul>
<p class="hint">(ab){n}, a{2,5} and caaaaaaaaaat, parentheses always needed for emoji</p>
</section>
<section class="regex-test slide" data-pattern="bo{0,}" data-test="boooo" title="Quantifier shortcuts">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>* = {0,} , + = {1,} , ? = {0,1}</li>
<li>Careful of accidental zero length matches!</li>
</ul>
</section>
<section class="regex-test slide" data-pattern="<.+>" data-test="<p>foo</p>" title="Greediness">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>Quantifiers are greedy</li>
<li>Lazify them by adding a ? after them</li>
</ul>
<p class="hint">Delete last > to show what happens</p>
</section>
<section class="regex-test slide" data-pattern="(b|d|f|g|l|m|p|r|t|y)uck" title="Character classes">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>Brackets = set, range or a combination of both</li>
<li>Concatenate multiple ranges for a union</li>
<li>Most metacharacters don’t need escaping in <code>[]</code></li>
</ul>
<p class="hint">in Unicode order</p>
</section>
<section class="slide" data-type="Challenge" data-duration="1" data-video="confused-kitten.webm">
<h1>Hex color</h1>
<p>
<code>#abc</code>, <code>#f00</code>, <code>#BADA55</code>, <code>#C0FFEE</code>
</p>
</section>
<section class="narrow slide" data-type="Solution">
<h1>Hex color</h1>
<pre><code class="language-javascript">/#[a-f0-9]{3,6}/i // Wrong!</code></pre>
<pre class="delayed persistent"><code class="language-javascript">/#([a-f0-9]{3}){1,2}/i</code></pre>
</section>
<section class="regex-test slide" data-pattern="[a-zA-Z0-9_]" title="Predefined character classes">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>
<code>\w</code> = <code>[a-zA-Z0-9_]</code>,
<code>\d</code> = <code>[0-9]</code>,
<code>\s</code> ≈ <code>[\t\r\n ]</code></li>
<li>Combine to form more complex character classes</li>
</ul>
</section>
<section class="slide" data-type="Example">
<h1>Counting words (roughly)</h1>
<pre><code class="language-javascript">function wordCount(text){
return text.match(/\w+/g).length;
}</code></pre>
<pre><code class="language-javascript">function wordCount(text){
return text.split(/\s+/).length;
}</code></pre>
</section>
<section class="slide" data-type="Challenge" data-duration="2" data-video="racoon.webm">
<h1>Number</h1>
<p>Without exponent or digit separators</p>
<p>
<code>-1</code> <code>.05</code> <code>+1000</code> <code>3.1415926535</code> <code>42.</code>
</p>
</section>
<section class="narrow slide" data-type="Solution">
<h1>Number</h1>
<pre><code class="language-javascript">/^[-+]?[\d.]+$/ // Too lax</code></pre>
<pre class="delayed persistent"><code class="language-javascript">/^[-+]?\d*\.?\d+$/ // False negatives: 5.</code></pre>
<pre class="delayed persistent"><code class="language-javascript">/^[-+]?\d*\.?\d*$/ // False positives: ., +., + etc</code></pre>
<pre class="delayed persistent"><code class="language-javascript">// Accurate, but is it worth it?
/^[-+]?(\d*\.?\d+|\d+\.)$/</code></pre>
</section>
<section class="regex-test slide" data-pattern="[\u0001-/:-@[-^`{-\uffff]" data-test="afz" title="Negated character classes">
<!-- [\u0001-/:-@[-^`{-\uffff] = \W -->
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li><code>^</code> negates a character class</li>
<li>
<code>\W</code> = <code>[^\w]</code>,
<code>\D</code> = <code>[^\d]</code>,
<code>\S</code> = <code>[^\s]</code>
</li>
<li>Even the dot is a character class: <code>.</code> = <code>[^\r\n\u2028\u2029]</code></li>
<li>DotAll alternatives: <code>[^]</code>, <code>[\S\s]</code>, <code>[\W\w]</code>, <code>[\D\d]</code></li>
</ul>
</section>
<section class="slide" data-type="Example">
<h1>Strip HTML</h1>
<pre><code class="language-javascript">function stripHTML(str){
return str.replace(/<.+?>/g, '');
}</code></pre>
<pre class="delayed"><code class="language-javascript">function stripHTML(str){
return str.replace(/<[^>]+>/g, '');
}</code></pre>
<p>Warning: Will fail in edge cases</p>
</section>
<section class="slide" data-type="Challenge" data-duration="1" data-video="munchkin.mp4">
<h1>Credit card numbers</h1>
<p>
<code>4060 1234 5678 9000</code>
<code>4060-1234-5678-3457</code>
<code>1230123456789123</code>
<code>4.060/123456-78 90-00</code>
<strong class="not">not</strong>
<code>hello</code>
<code>4060</code>
<code>12345678901234567890</code>
</p>
</section>
<section class="narrow slide" data-type="Solution">
<h1>Credit card numbers</h1>
<pre><code class="language-javascript">// Never do this!
/\d{16}/</code></pre>
<pre class="delayed persistent"><code class="language-javascript">// Limited grouping + allows 19 digit numbers
/(\d{4}.?){3}\d{4}/</code></pre>
<pre class="delayed persistent"><code class="language-javascript">/(\d\D*){16}/</code></pre>
</section>
<section class="regex-test slide" data-pattern="\p{Script=Cyrillic}+" data-test="Веру Лиа" data-flags="gu" title="Unicode Property Escapes">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>Syntax: <code>\p{<em>UNICODE_PROPERTY</em>=<em>VALUE</em>}</code>. Needs <code>u</code> flag.</li>
<li>When Unicode Property is <code>General_Category</code> it can be omitted.</li>
<li>Experimental, <a href="https://github.com/tc39/proposal-regexp-unicode-property-escapes">proposal in Stage 3</a>, implemented in Chrome & Safari</li>
</ul>
<p class="hint">Script=Cyrillic</p>
</section>
<section class="regex-test slide" data-pattern="(Java|ECMA)Script" data-test="JavaScript" title="Capturing">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>Parentheses form capturing groups</li>
<li>Add <code>?:</code> in the begninning to avoid this</li>
</ul>
</section>
<section class="regex-test slide" data-pattern="^a" data-test="cat" title="Anchors">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li><code>^</code> = beginning of string</li>
<li><code>$</code> = end of string</li>
<li>beginning/end of lines, with the <code>m</code> flag</li>
</ul>
</section>
<section class="slide" data-type="Challenge" data-duration="2" data-video="goats.webm">
<h1>ISO 8601 Dates</h1>
<p>Just dates, no time or timezone information</p>
<p>
<code>2012-12-12</code>, <code>1986-06-13</code>
</p>
</section>
<section class="slide" data-type="Solution">
<h1>ISO 8601 Dates</h1>
<pre><code class="language-javascript">/^\d{4}-\d{2}-\d{2}$/.test(str)</code></pre>
<pre class="delayed persistent"><code class="language-javascript">/^\d{4}-(0\d|1[0-2])-([0-2]\d|3[01])$/.test(s)</code></pre>
<p class="delayed">Can it be improved further?</p>
</section>
<section class="slide" data-type="Example">
<h1>Trimming a string</h1>
<pre><code class="language-javascript">if (!String.prototype.trim) {
String.prototype.trim = function(){
return this.replace(/^\s+|\s+$/g, '');
}
}</code></pre>
</section>
<section class="regex-test slide" data-pattern="\b" title="Assertions">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>Assertions are zero width</li>
<li><code>\b</code> = word boundary = between <code>\w</code> and <code>\W|^|$</code></li>
<li><code>\B</code> = non-word boundary = between <code>\w</code> and <code>\w</code> or <code>\W</code> and <code>\W</code></li>
</ul>
</section>
<!-- <article class=" slide language-js">
<h1>Before <code>element.classList</code>…</h1>
<pre><code>function removeClass(element, name) {
var regex = RegExp("\\b" + name + "\\b", "g");
return element.className = element.className.replace(regex, ""));
}</code></pre>
<pre class="delayed"><code>element.className = "item-toolbar";
removeClass(element, "toolbar"); // "item-"</code></pre>
</article> -->
<section class="regex-test slide" data-pattern="a(b)" data-test="ab" title="Lookahead">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li><code>(?=a)</code> = followed by a, which can be any regex</li>
<li><code>(?!a)</code> = NOT followed by a</li>
</ul>
</section>
<section id="lookahead-hacks" class="language-javascript">
<header class="slide">
<h1>Lookahead hacks</h1>
<h2>for fun and profit</h2>
</header>
<section class="slide">
<h1>Intersection <span class="math"><em>A</em> ∩ <em>B</em></span></h1>
<p>“Password must be 6 letters or longer, and must contain at least one number, one letter, and one symbol.”</p>
<pre><code>password.length > 6
&& /\d/.test(password) // has digit?
&& /[a-z]/i.test(password) // has letter?
&& /\W/.test(password) // has symbol?</code></pre>
<pre class="delayed"><code>// Or, with lookaheads…
/^(?=.*\d)(?=.*[a-z])(?=.*[\W_]).{6,}$/i</code></pre>
</section>
<section class="slide">
<h1>Subtraction <span class="math"><em>A</em> – <em>B</em></h1>
<p>“Any integer that’s NOT divisible by 50”</p>
<pre><code> /^(?!\d+[50]0)\d+$/</code></pre>
</section>
<section class="slide">
<h1>Negation <span class="math"><em>A̅</em></h1>
<p>“Anything that doesn’t contain "foo"”</p>
<pre><code>/^(?!.*foo).+$/</code></pre>
</section>
</section>
<section class="regex-test slide" data-pattern="(?<=a)b" data-test="ab" title="Lookbehind">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li><code>(?<=a)</code> = preceded by a, which can be any regex</li>
<li><code>(?<!a)</code> = NOT preceded by a</li>
<li>Experimental (<a href="https://github.com/tc39/proposal-regexp-lookbehind">Proposal in Stage 3</a>), only supported in V8 behind a flag</li>
</ul>
</section>
<section class="regex-test slide" data-pattern="('|").+?('|")" title="Backreferences">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li><code>\N</code> = Nth backreference</li>
</ul>
<p class="hint">('|")(\\\1|.)+?\1 for escaped quotes</p>
</section>
<section class="regex-test slide" title="Account for escaped quotes AND backslashes"
data-pattern="("|')(\\?.)*?\1"
data-test='"He 'said' \"hi\"!" "another string"'>
<p class="credit">Regex credit: Steven Levithan</p>
</section>
<section class="slide" data-type="Challenge" data-duration="2" data-video="maru.webm">
<h1>Correctly Nested Parens</h1>
<p>
<code>()</code>, <code>(())()</code>, <code>(()()())</code>, <code>((((((()))))))</code>
<strong class="not">not</strong> <code>()(</code>, <code>)</code>, <code>((((((()))</code>
</p>
</section>
<article class="top-heavy image slide" id="troll">
<h1 class="delayed">Not a regular language</h1>
</article>
<article class="slide" id="cfg">
<h1>Context Free Grammar</h1>
<ul class="plain math">
<li><em>S</em> → (<em>S</em>)</li>
<li><em>S</em> → <em>S</em><em>S</em></li>
<li><em>S</em> → <em>ε</em></li>
</ul>
</article>
</section>
<section>
<header class="slide" id="best-practices">
<h1>Best Practices</h1>
</header>
<section class="image slide" id="precise-impractical" data-source="regular-expressions.info/email.html">
<code>(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])</code>
<h1 class="caption">Practicality > Precision</h1>
</section>
<section class="narrow slide" style="min-width: 20em">
<h1>Email</h1>
<pre><code class="language-javascript">// Don’t ever do this
/^[a-z]+@[a-z]+\.[a-z]{2,5}/</code></pre>
<p class="match-examples delayed persistent">
<code>[email protected]</code>
<code>[email protected]</code>
<code>[email protected]</code>
<code>Лиа@Веру.рф</code>
<code>λία@βέρου.ελ</code>
<code>[email protected]</code>
</p>
<pre class="delayed persistent"><code class="language-javascript">// Yes, it’s ok to be lax!
/^\S+@\S+\.\S+/</code></pre>
</section>
<section class="top-heavy image slide" style="background-image: url(img/pregnancy-test.jpg);" data-source="http://www.flickr.com/photos/mandaloo/4369363690/">
<h1>False positives vs false negatives</h1>
</section>
<section class="image slide language-javascript" id="kiss">
<div class="caption">
<h1>Keep it simple</h1>
<h2>Even if it means not using regexes at all</h2>
</div>
<ul class="plain delayed-children js-vs-regex">
<li>
<code>str === "foo"</code>
<strong>></strong>
<code>/^foo$/.test(str)</code>
</li>
<li>
<code>str.indexOf("foo") > -1</code>
<strong>></strong>
<code>/foo/.test(str)</code>
</li>
<li>
<code>str.indexOf("foo") === 0</code>
<strong>></strong>
<code>/^foo/.test(str)</code>
</li>
</ul>
</section>
<section class="slide">
<h1>For performance</h1>
<ul>
<li>Avoid greedy quantifiers</li>
<li>Don’t forget anchors (<code>^</code> and <code>$</code>)</li>
<li>Be as specific as possible</li>
<li>Prefer non-capturing groups <code>(?:)</code></li>
<li>Minimize backtracking</li>
</ul>
</section>
<section class="regex-test slide" data-pattern="(x+x+)+y" data-test="xxxx" title="Catastrophic backtracking">
<h1>What we learned</h1>
<ul class="delayed-children persistent">
<li>Only applies when there is no match</li>
<li>Avoid nested quantifiers</li>
</ul>
</section>
</section>
<!-- <section>
<header class="slide" id="js-regexp-api">
<h1>JS RegExp API</h1>
</header>
<section class="slide">
<h1>RegExp instances</h1>
<pre><code class="language-javascript">var literal = /^\$\d+/g,
// You don’t need the new operator
dynamic = RegExp('^\\$\\d+', 'g');</code></pre>
<ul class="delayed">
<li>Literals are simpler, faster, more concise</li>
<li>Only use the constructor when you really have to!</li>
</ul>
</section>
<section class="slide">
<h1>RegExp properties for flags</h1>
<ul>
<li>
<code>RegExp#global</code>
</li>
<li>
<code>RegExp#multiline</code>
</li>
<li>
<code>RegExp#ignoreCase</code>
</li>
</ul>
<pre><code class="language-javascript">>>> /a/g.global; // true
>>> /a/.global; // false
>>> /a/m.multiline; // true
>>> /a/gi.multiline; // false
>>> /a/i.ignoreCase; // true</code></pre>
</section>
<section class="slide">
<h1>Other RegExp properties</h1>
<ul>
<li>
<code>RegExp#source</code>
<pre><code class="language-javascript">console.log(/a/i.source) // "a"</code></pre>
</li>
<li><code>RegExp#lastIndex</code></li>
</ul>
</section>
<section class="slide" data-import="methods"></section>
<section class="slide">
<h1><code>RegExp#test</code></h1>
<pre><code class="language-javascript">/a/.test('ab'); // returns true</code></pre>
<ul>
<li>Faster than most of the others</li>
</ul>
</section>
<section class="slide">
<h1><code>RegExp#exec</code></h1>
<pre><code class="language-javascript">/a(b)/.exec('abba'); // ["ab", "b"]</code></pre>
<pre class="delayed"><code class="language-javascript">var reg = /a(b)/g, bar = 'abba', matches = [], m;
while (m = reg.exec(bar))
matches.push({
index: m.index, // reg.lastIndex-m[0].length+1
length: m[0].length,
groups: m
});</code></pre>
</section>
<section class="slide">
<h1><code>RegExp#exec</code></h1>
<pre><code class="language-javascript">>>> /a(bc)/.exec('abcba')[1];
"bc"</code></pre>
<pre><code class="language-javascript">>>> /a(bc)/.exec('abba')[1];</code>
<code class="error">TypeError: Cannot read property '1' of null</code></pre>
<div class="delayed">
<p>Handy trick by James Padolsey:</p>
<pre><code class="language-javascript">>>> (/regex/.exec('string') || [,''])[1];</code></pre>
</div>
</section>
<section class="slide">
<h1><code>String#match</code></h1>
<pre><code class="language-javascript">>>> 'abcdef'.match(/(\w){3}/);
["abc", "c"] // Same as /(\w){3}/.exec('abcdef')</code></pre>
<pre><code class="language-javascript">>>> 'abcdef'.match(/(\w){3}/g);
["abc", "def"]</code></pre>
<pre><code class="language-javascript">>>> 'ab'.match(/(\w){3}/g);
null</code></pre>
</section>
<section class="slide">
<h1><code>String#replace</code></h1>
<pre><code class="language-javascript">>>> 'blabla'.replace('b', ''); // "labla"</code></pre>
<pre class="delayed persistent"><code class="language-javascript">>>> 'blabla'.replace(/b/g, ''); // "lala"</code></pre>
<pre class="delayed persistent"><code class="language-javascript">>>> 'blabla'.replace(/(\w)/g, '$1$1'); // "bbllaabbllaa"</code></pre>
<pre class="delayed persistent"><code class="language-javascript">>>> 'border-radius'.replace(/-(\w)/g, function($0,$1){
return $1.toUpperCase();
}); // "borderRadius"</code></pre>
</section>
<section class="slide">
<h1><code>String#split</code></h1>
<pre><code class="language-javascript">'Lea wants a $20K Mini.'.split(/\s+/).length; // 5</code></pre>
<h1><code>String#search</code></h1>
<pre><code class="language-javascript">'Lea wants a $20K Mini.'.search(/\s+/); // 3</code></pre>
<ul class="delayed">
<li>Like a regex-aware <code>String#indexOf</code>!</li>
</ul>
</section>
</section> -->
<!-- <section>
<header class="slide" id="tools">
<h1>Tools</h1>
</header>
<section class="slide">
<hgroup>
<h1>XRegExp</h1>
<h2>By Steven Levithan</h2>
</hgroup>
<p>RegExp wrapper that adds awesomeness like:</p>
<ul>
<li>Comments in regexes</li>
<li>Flag to turn off automatic capturing</li>
<li>Flag to ignore whitespace</li>
<li>Named capturing groups</li>
<li>Unicode-aware predefined classes</li>
<li>Smooths out browser bugs</li>
<li>Plugins for even more awesomeness</li>
</ul>
</section>
<section class="slide" data-src="http://xregexp.com/"></section>
<section class="slide">
<hgroup>
<h1>Regexpal</h1>
<h2>By Steven Levithan</h2>
</hgroup>
<p>Simple but powerful regexp tester</p>
<ul>
<li>Highlights regex syntax and matches as you type</li>
<li>Includes a quick reference for regex syntax</li>
<li>Knows about syntax that doesn’t work correctly in some browsers, and marks it as an error</li>
<li>Pure JavaScript, so you can save it and use offline</li>
</ul>
</section>
<section class="slide" data-src="http://regexpal.com/"></section>
<section class="slide" data-src="http://www.regexbuddy.com/"></section>
<section class="slide">
<h1>Resources</h1>
<h2>Online:</h2>
<ul style="word-break: break-all;">
<li><a href="http://www.regular-expressions.info">regular-expressions.info</a></li>
<li><a href="http://blog.stevenlevithan.com/">blog.stevenlevithan.com</a>
<li><a href="https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions">developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions</a></li>
</ul>
<h2>Books:</h2>
<ul>
<li>Regular Expressions Cookbook - Jan Goyvaerts & Steven Levithan</li>
<li>Mastering Regular Expressions - Jeffrey E.F. Friedl</li>
</ul>
</section>
</section> -->
<article class="slide" id="video-credits">
<h1>Video Credits</h1>
<ul class="plain">
<li><a href="https://www.youtube.com/watch?v=IwtdIXWGry4">Melanie Raccoon riding bike-side angle</a></li>
<li><a href="https://twitter.com/AMAZlNGNATURE/status/938663598263484416">Living the dream</a></li>
<li><a href="https://www.youtube.com/watch?v=mt084vYqbnY">Hamster Has Epic Fail on Running Wheel</a></li>
<li><a href="https://www.youtube.com/watch?v=RN50R3gycgo&feature=youtu.be&t=7">Goat Kid Pajama Party</a></li>
<li><a href="https://www.youtube.com/watch?v=xdhLQCYQ-nQ">大きな箱とねこ。</a></li>
<li><a href="https://twitter.com/videocats/status/940154890004942848">Shiba Munchkin</a></li>
<li><a href="https://www.youtube.com/watch?v=HECa3bAFAYk">Fluffy kitten is confused</a></li>
<li><a href="https://twitter.com/videocats/status/938482862680133632">Learning to drink</a></li>
</ul>
</article>
<article class="slide" id="thankyou" title="Thank you!" data-credits="gifimage.net, giphy.com"></article>
<footer class="slide dont-resize">
<img src="../talks/shared/img/mark.svg" class="logo" />
<h1>Спасибо!</h1>
<img src="../talks/css-variables/img/stickers.jpg" class="stickers delayed" alt="">
<p class="stickercaption delayed">Want one? Just ask!---></p>
<ul class="plain">
<li style="--icon: '🌍'"><a href="http://mavo.io">lea.verou.me</a></li>
<li style="--icon: '✉'"><a href="mailto:[email protected]">[email protected]</a></li>
<li style="--icon: '🐦'"><a href="http://twitter.com/leaverou">@leaverou</a></li>
</ul>
<p>Slides: <a href="https://leaverou.github.io/regexplained/slides.html">leaverou.github.io/regexplained/slides.html</a></p>
<p style="font-size: 50%;">This slide deck was entirely built with open web technologies! Slideshow framework: <a href="https://github.com/LeaVerou/CSSS">github.com/LeaVerou/CSSS</a></p>
</footer>
<script src="bliss.shy.js"></script>
<script>
// Bliss.$('body.no-challenges .slide[data-type="Solution"]').forEach(function(slide) {
// // Turn challenge answers into examples
// var challengeSlide = slide.previousElementSibling;
// var h1 = $('h1', slide);
// var sibling = h1? h1.nextSibling : null;
//
// Bliss.$('p', challengeSlide).forEach(function(p) {
// slide.insertBefore(p, sibling);
// });
//
// challengeSlide.parentNode.removeChild(challengeSlide);
// slide.setAttribute('data-type', 'Example');
// });
</script>
<script src="../csss/slideshow.js"></script>
<script>
var $ = Bliss, $$ = Bliss.$;
</script>
<script src="regex-test.js"></script>
<script src="regexplained.js"></script>
<script src="../prism.js/prism.js"></script>
<script>
$$(".slide.regex-test[title]:not([id])").forEach(slide => slide.id = slide.title.replace(/[^\w\s-]/g, "").replace(/\s+/g, "-").toLowerCase())
</script>
</body>
</html>