<div dir="ltr"><div>Hello,</div><div><br></div><div>This is my write-up for HW#N</div><div><br></div><div>I went to the &quot;A Gentle Introduction to Regular Expression in JavaScript&quot; event organized by NY Javascript group of <a href="http://meetup.com">meetup.com</a> this Thursday. As a qualified software developer, regular expression is one of the skills which must be grasped. Especially for system administrators, they have to be proficient in regular expression. When writing the scripts for server management automation, extracting the log info for issue tracking or software developing, in-depth understanding of regular expression can greatly improve the working efficiency. The speaker in this event introduces the basic concept of regular expression and provides several examples of JavaScript to demonstrate its applications.<br></div><div><br></div><div><div><b>The most popular applications of regular expression involve:</b></div><div><ul><li>Validation<br></li><li>Route Matching<br></li><li>Search &amp; Replace(substitution)<br></li><li>Parse<br></li></ul><div><b>The basics of regular expression</b></div></div></div><div><div><ul><li>  .            matches ANY character, EXCEPT a line breaks. equiv to [^\n\r]<br></li><li>  \s\S       matches ANY character, INCLUDING line breaks. equiv to [^]<br></li><li>  \w          matches ALPHANUMERIC and UNDERSCORE. equiv to [A-Za-z0-9_]<br></li><li>  \W         matches NON-ALPHANUMERIC or UNDERSCORE. equiv to [^A-Za-z0-9_]<br></li><li>  \d          matches NUMBERS aka DIGITS. equiv to [0-9]<br></li><li>  \D         matches NON-NUMBERS aka NON-DIGITS. equiv to [^0-9]<br></li><li>  \s          matches ANY WHITESPACE (spaces, tabs, line breaks)<br></li><li>  \S         matches ANY NON-WHITESPACE<br></li><li>  [abc]     matches ANY characters in-between the square brackets<br></li><li>  [^abc]    matches ALL EXCEPT characters in-between the square brackets<br></li><li>  [a-z]      matches a RANGE between two characters<br></li></ul><div><b>Best practices:</b></div></div></div><div><div><ul><li>Match only what&#39;s necessary, too loose expression cannot match all results you want, but too strict expression is hard to be implemented and is obscure.<br></li><li>Don&#39;t bother trying to parse HTML.<br></li><li>Your expression should be good at doing a single task.<br></li></ul><div><b>Two major examples in the meetup</b></div></div></div><div><b><br></b></div><div>    1. Extract video id from youtube url</div><div><br></div><div>          var url = &quot;<a href="https://youtube.com/watch?v=dQw4w9WgXcQ">https://youtube.com/watch?v=dQw4w9WgXcQ</a>&quot;;</div><div>          url.match(/.+:\/\/.+\/.+v?=(\w+)/).pop()</div><div>          &gt; &quot;dQw4w9WgXcQ&quot;</div><div>          </div><div>          we use group (\w+) to extract the partial info from the matched string</div><div><br></div><div>     2. Write a regular expression to match the email address</div><div><br></div><div>          Too loose implementation: ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$, but it cannot match &quot;<a href="mailto:lee@mail.museum">lee@mail.museum</a>&quot;</div><div>          </div><div>          Too strict implementation(official standard: RFC 5322):  </div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>   (?:[a-z0-9!#$%&amp;&#39;*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&amp;&#39;*+/=?^_`{|}~-]+)*</div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>  |  &quot;(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]</div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>      |  \\[\x01-\x09\x0b\x0c\x0e-\x7f])*&quot;)</div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>@ (?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?</div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>  |  \[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}</div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>       (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:</div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>          (?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]</div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>          |  \\[\x01-\x09\x0b\x0c\x0e-\x7f])+)</div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>     \])</div></div></blockquote></blockquote><div>            </div><div>          More practical implementation: ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$</div><div><br></div><div><br></div><div><div><b>Event slides: <a href="http://moimikey.github.io/a-gentle-intro-to-regex/#/">http://moimikey.github.io/a-gentle-intro-to-regex/#/</a></b></div><div><b><br></b></div><div><b>github: <a href="https://github.com/moimikey/a-gentle-intro-to-regex">https://github.com/moimikey/a-gentle-intro-to-regex</a></b></div></div><div><b><br></b></div><div><b>Slug Challenge: <a href="https://gist.github.com/jdaudier/743d2b56091e688702d8">https://gist.github.com/jdaudier/743d2b56091e688702d8</a>, a challenge should be completed using regular expression if you have interest.</b></div><div><b><br></b></div><div>Lingfei Hu<span id="goog_428692232"></span><span id="goog_428692233"></span><a href="/"></a></div><div>Thanks</div><div><br></div></div>