-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
374 lines (353 loc) · 20.6 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Girl Develop It - Intermediate JavaScript</title>
<meta name="description" content="Intermediate JavaScript course to follow and expand upon the Introduction to JavaScript">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/gdilight.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor--><link rel="stylesheet" href="lib/css/light.css">
<!-- dark editor<link rel="stylesheet" href="lib/css/dark.css">
<link rel="stylesheet" href="lib/css/zenburn.css">-->
<link rel="stylesheet" href="plugin/accessibility-helper/css/accessibility-helper.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="css/print/pdf.css" type="text/css" media="print">
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Opening -->
<section>
<h2>Fun with Regular Expressions</h2>
<h3>(no, really)</h3>
</section>
<section>
<p>Based on...</p>
<h2>Intermediate JavaScript</h2>
<h3>Girl Develop It Philly</h3>
<p><a href="http://gdiphilly.github.io/Intermediate-JS" target="_blank">http://gdiphilly.github.io/Intermediate-JS</a></p>
</section>
<section>
<h3>This talk...</h3>
<ul>
<li>is fairly beginner friendly</li>
<li>may help you find a new appreciation for something you don't like much</li>
<li>welcomes questions!</li>
<li>is available at <a href="http://github.com/EliseWei/regex-presentation" target="_blank">github.com/EliseWei/regex-presentation</a></li>
</ul>
</section>
<section>
<h3>Elise Wei</h3>
<img src="img/elise_profile_Aug2016.jpg" alt="Photo of Elise" class="left" style="max-width: 30%" />
<ul class="right" style="max-width: 65%; margin-top: 1em;">
<li>Drexel grad</li>
<li>Self-taught (2002-present)</li>
<li>Full-time dev (2008-present)</li>
<li>Girl Develop It (2012-present)</li>
</ul>
</section>
<section>
<h3>Elise Wei</h3>
<p>Software Engineer at Ticketleap, working on <a href="http://sellwithport.com/" target="_blank">Port</a> (currently in beta).</p>
</section>
<section>
<p>
<strong>Monetate</strong>: 3rd party script that allows marketers to personalize/customize their e-commerce sites and measure the impact.
</p>
<h3 class="fragment">
Scraping!
</h3>
</section>
<!-- Regular Expressions -->
<section>
<h3>Regular Expressions</h3>
<p>Allow us to match patterns (sometimes extremely complex) in strings.</p>
</section>
<section>
<h3>What do you do with them?</h3>
<p class="left-align">Given some text and a pattern, you can "split" "replace" "match" "search" "test" and "exec." But the <span class="red">how</span> is a bit quirky.</p>
</section>
<section>
<h3>String methods that accept regexes</h3>
<ul>
<li><span class="red">split</span>: finds all instances of the pattern, deletes them, and uses those points to divide text into an array</li>
<li><span class="red">replace</span>: Can be a simple replacement or can be more complex</li>
<li><span class="red">match</span>: returns an array of all matching substrings</li>
<li><span class="red">search</span>: returns the numerical index of the first matched pattern or -1 if not matched</li>
</ul>
<pre><code class="javascript">
var str = 'which way';
str.search(/hi/); // returns 1
</code></pre>
</section>
<section>
<h3>Regex methods that accept strings</h3>
<ul>
<li><span class="red">test</span>: returns true if the pattern is found, otherwise, false</li>
<li><span class="red">exec</span>: returns only the first matched string and captured substring if applicable (this is different from "match" if using the global modifier. More on that later.)</li>
</ul>
<pre><code class="javascript">
var pattern = /hi/;
pattern.test('chocolate chip'); // returns true
</code></pre>
</section>
<section>
<h3>JS is like...</h3>
<img src="http://inthemixxshow.com/wp-content/uploads/2014/02/plaza-gif.gif" alt="" />
</section>
<section>
<h3>And the devs are like...</h3>
<img src="https://lh3.googleusercontent.com/-yjYjiYXdCGA/VcCwd-2cu1I/AAAAAAAAUHs/B4Ms00N0eUI/w500-h281/tswift_awkhands.gif" alt="" />
</section>
<section>
<p>Regexes are VERY POWERFUL, but can be difficult to use.</p>
<blockquote class="fragment">Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. —Jamie Zawinski</blockquote>
</section>
<section>
<h3>So really, what do you do with them?</h3>
<p class="left-align">Fun/silly stuff:</p>
<ul style="width: 100%">
<li><a href="https://chrome.google.com/webstore/detail/millennials-to-snake-peop/jhkibealmjkbkafogihpeidfcgnigmlf?hl=en-US" target="_blank">Snake People</a></li>
<li><a href="https://chrome.google.com/webstore/detail/drumpfinator/hcimhbfpiofdihhdnofbdlhjcmjopilp" target="_blank">Drumpfinator</a></li>
</ul>
<p class="left-align"><br />Real uses:</p>
<ul>
<li>sanitizing and validating user-entered data</li>
<li>automatically suggesting search terms or tags for a new blog post</li>
<li>templating with strings (text replacement)</li>
<li>getting data out of a url</li>
<li>and many more</li>
</ul>
</section>
<section>
<p>For the following examples, you can use <a href="https://regex101.com/#javascript" target="_blank">regex101</a> to easily experiment.</p>
</section>
<section>
<h3>The basics of writing a regular expression</h3>
<pre><code class="javascript">
'Hello, class'
</code></pre>
<ul>
<li>Start and end with forward slashes <code>/a/</code>, or instantiate with <code>new RegExp('a')</code>. Either would match the first lowercase "a"</li>
<li>Can be used to find an exact string, such as <code>/Hello/</code></li>
<li>Can look for specific character sets at the beginning and end of lines using <code>^</code> and <code>$</code> (which you may recognize from some CSS uses) <code>/^class/</code> would not find a match, but <code>/class$/</code> would</li>
</ul>
</section>
<section>
<h3>Escaping & character ranges</h3>
<pre><code class="javascript">
'$50.00'
</code></pre>
<div class="code-list left-align">
<p><code>/\$/</code>We've already seen that the dollar sign has special meaning in a regular expression, so what if we're actually looking for a dollar sign character? Use a backslash</p>
<p><code>[0123456789]</code>You can also match any of a group of characters by using square brackets</p>
<p><code>[0-9]</code>But you can also signify any of a range of characters by using a hyphen</p>
</div>
</section>
<section>
<h3>Escaping & character ranges</h3>
<div class="code-list left-align">
<p><code>[0-9a-z]</code>Or multiple ranges.<br />
This signifies a range of <a href="http://unicode-table.com/en/#basic-latin" target="_blank">unicode characters</a>, by number.</p>
<p class="fragment red">What's the difference between [A-z] and [A-Za-z]?</p>
<p class="fragment"><code>[^13579]</code>You can also use a carat to exclude characters in a range. This example would match only even digits</p>
</div>
</section>
<section>
<h3>One of...</h3>
<div class="code-list left-align">
<p><code>x|y|z</code>Any of the pipe-separated options<br />
Here, there is no difference from [xyz], but it becomes more useful when using longer expressions, for example x1|y2|z3.</p>
</div>
</section>
<section>
<h3>Wildcards</h3>
<p class="left-align">There are also some special wildcards that you can use to find pre-defined character sets:</p>
<div class="left-align">
<p><code>.</code>A period matches any character. Escape with a backslash to match an actual period.</p>
<p><code>\s</code> for whitespace (<code>\S</code> for non-whitespace) Whitespace: spaces, tabs, new lines, CRs, etc</p>
<p><code>\d</code> for digits (<code>\D</code> for non-digits)</p>
<p><code>\w</code> for "word" characters, which includes letters, digits, and the underscore (<code>\W</code> for non-word)</p>
<p><code>\b</code> for the beginnings or ends of words (<code>\B</code> for everything else)</p>
</div>
</section>
<section>
<h3>Modifiers</h3>
<p class="left-align">Modifiers added after the final slash signify global, case-insensitive, and multi-line searches that will match the start and end of a line as if it were the start or end of a string (g, i, m). There are a couple other modifiers for unicode and 'stickiness,' but they're not used often.</p>
</section>
<section>
<h3>Modifiers</h3>
<pre><code class="javascript" contenteditable>
'Hello, class.
How was your week?'
</code></pre>
<div class="left-align">
<p><code>/h/</code> would have no matches.</p>
<p><code>/h/i</code> would match the first "H"</p>
<p><code>/h/ig</code> would match all instances of "H". This is useful for "match" and "replace" functions. "split" is global by default, and "test" and "search" are not affected by a global modifier.</p>
<p><code>/^h/ig</code> would match only the first "H", while <code>/^h/igm</code> would match both.</p>
</div>
</section>
<section>
<h3>Capturing groups</h3>
<p class="left-align">Sometimes we'll want to look for a pattern, but only use part of it. Parentheses indicate a part of the pattern you would specifically like the search to "capture" and return.</p>
<pre><code class="javascript" contenteditable>
"ace".match(/a(c)e/); // returns ["ace", "c"]
</code></pre>
<p class="left-align">The whole pattern is matched, and the next element of the returned array is the specifically "captured" group of matched characters.</p>
</section>
<section>
<h3>Capturing groups</h3>
<p class="left-align">Now combine that with the character range syntax <code>/a([a-z])e/</code></p>
<pre><code class="javascript" contenteditable>
var pattern = /a([a-z])e/;
"age".match(pattern); // returns ["age", "g"]
"ale".match(pattern); // returns ["ale", "l"]
"variegated".match(pattern); // returns ["ate", "t"]
</code></pre>
</section>
<section>
<h3>Are you beginning to see the awesome power?</h3>
<img src="https://adprice.fedorapeople.org/img/omg_cat.jpg" alt="mind. blown." />
</section>
<section>
<h3>Quantifiers</h3>
<div class="left-align">
<p>You can also specify different numbers of characters to allow in the match.</p>
<p><code>+</code> Allows for any (non-zero) number of the preceding character or expression to qualify for the match.</p>
<p><code>*</code> Looks for zero or more of the preceding character or expression</p>
<p><code>?</code> Matches exactly 0 or 1 of something</p>
<p><code>{3} {1,3}</code> Match a specific quantity, or a specific quantity range.</p>
</section>
<section>
<h3>Quantifiers</h3>
<p>This looks like...</p>
<pre><code class="javascript" contenteditable>
"variegated".match(/a([a-z]+)e/); // returns ["ariegate", "riegat"]
</code></pre>
<p>(This might not be what you expected. Watch out for "greediness!" use <code>+?</code>, <code>*?</code> or <code>{}?</code> to create a "reluctant", rather than greedy search)</p>
<pre><code class="javascript" contenteditable>
"variegated".match(/a([a-z]+?)e/); // returns ["arie", "ri"]
</code></pre>
</section>
<section>
<section>
<h2>Bonus</h2>
<ul>
<li>parens (and non-capturing)</li>
<li>deciphering regex</li>
<li>look-ahead</li>
<li>multi-part search/replace</li>
</ul>
</section>
<section>
<h3>Parentheses can...</h3>
<ul>
<li>Create a capturing group</li>
<li>Apply modifiers to a group of characters</li>
<li>Group multi-character options</li>
</ul>
<pre><code class="javascript" contenteditable>
/a([a-z])e/ // matches ace, ate, ale and returns the middle letter
/a(pa)?ce/ // matches ace or apace and returns "pa" or ""
/a(cr|bl)e/ // matches acre or able and returns "cr" or "bl"
</code></pre>
</section>
<section>
<h3>Non-capturing parens</h3>
<p class="left-align">
In cases 2 and 3, if you want parentheses for grouping, but don't need the result, use <code>?:</code> inside the parens to indicate a non-capturing group.
</p>
<pre><code class="javascript" contenteditable>
/a(?:[a-z])e/ // this is the same as leaving out the parens entirely
/a(?:pa)?ce/ // matches ace or apace and returns nothing
/a(?:cr|bl)e/ // matches acre or able and returns nothing
</code></pre>
</section>
<section>
<h3>Deciphering Regexes</h3>
<pre><code class="javascript" contenteditable>
/(\d+)[\,\.](?=\d{3})/
</code></pre>
<div class="left-align">
<p class="fragment"><code>(\d+)</code> Capture one or more digits</p>
<p class="fragment"><code>[\,\.]</code> A period or comma</p>
<p class="fragment"><code>(?= )</code> Followed by... (<a href="http://www.regular-expressions.info/lookaround.html" target="_blank">more on looking ahead/back</a>)</p>
<p class="fragment"><code>\d{3}</code> Exactly 3 digits</p>
</div>
</section>
<section>
<h3>Complex Replacement</h3>
<pre><code class="javascript" contenteditable>
var pattern = /(\d+)[\,\.](?=\d{3})/g;
function stripSeparators(numStr) {
return numStr.replace(pattern, "$1");
}
</code></pre>
<p class="left-align fragment">
Each time the full pattern is matched, it is replaced with the first captured group (element 1 of the array returned by the pattern match).
</p>
</section>
</section>
<section>
<h3>Summary</h3>
<p class="left-align">
Regular expressions can be incredibly useful and powerful. When you start small and get familiar, they can even be fun!
</p>
</section>
<section>
<h2>Contact</h2>
<ul>
<li>phillydev slack</li>
<li><a href="https://twitter.com/Not_Pele" target="_blank">@not_pele</a></li>
<li>[email protected]</li>
<li><a href="https://www.linkedin.com/in/elisewei" target="_blank">LinkedIn</a></li>
</ul>
</section>
</div>
<footer style="background-image: none"></footer>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'fade', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/accessibility-helper/js/accessibility-helper.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>