[cs615asa] The Homework N Meet-up Report

Lingfei Hu lhu4 at stevens.edu
Sat Apr 25 22:21:59 EDT 2015


Hello,

This is my write-up for HW#N

I went to the "A Gentle Introduction to Regular Expression in JavaScript"
event organized by NY Javascript group of meetup.com 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.

*The most popular applications of regular expression involve:*

   - Validation
   - Route Matching
   - Search & Replace(substitution)
   - Parse

*The basics of regular expression*

   -   .            matches ANY character, EXCEPT a line breaks. equiv to
   [^\n\r]
   -   \s\S       matches ANY character, INCLUDING line breaks. equiv to [^]
   -   \w          matches ALPHANUMERIC and UNDERSCORE. equiv to
   [A-Za-z0-9_]
   -   \W         matches NON-ALPHANUMERIC or UNDERSCORE. equiv to
   [^A-Za-z0-9_]
   -   \d          matches NUMBERS aka DIGITS. equiv to [0-9]
   -   \D         matches NON-NUMBERS aka NON-DIGITS. equiv to [^0-9]
   -   \s          matches ANY WHITESPACE (spaces, tabs, line breaks)
   -   \S         matches ANY NON-WHITESPACE
   -   [abc]     matches ANY characters in-between the square brackets
   -   [^abc]    matches ALL EXCEPT characters in-between the square
   brackets
   -   [a-z]      matches a RANGE between two characters

*Best practices:*

   - Match only what's necessary, too loose expression cannot match all
   results you want, but too strict expression is hard to be implemented and
   is obscure.
   - Don't bother trying to parse HTML.
   - Your expression should be good at doing a single task.

*Two major examples in the meetup*

    1. Extract video id from youtube url

          var url = "https://youtube.com/watch?v=dQw4w9WgXcQ";
          url.match(/.+:\/\/.+\/.+v?=(\w+)/).pop()
          > "dQw4w9WgXcQ"

          we use group (\w+) to extract the partial info from the matched
string

     2. Write a regular expression to match the email address

          Too loose
implementation: ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$, but it cannot
match "lee at mail.museum"

          Too strict implementation(official standard: RFC 5322):

   (?:[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])+)

     \])


          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)$


*Event slides: http://moimikey.github.io/a-gentle-intro-to-regex/#/
<http://moimikey.github.io/a-gentle-intro-to-regex/#/>*

*github: https://github.com/moimikey/a-gentle-intro-to-regex
<https://github.com/moimikey/a-gentle-intro-to-regex>*

*Slug Challenge: https://gist.github.com/jdaudier/743d2b56091e688702d8
<https://gist.github.com/jdaudier/743d2b56091e688702d8>, a challenge should
be completed using regular expression if you have interest.*

Lingfei Hu
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.stevens.edu/pipermail/cs615asa/attachments/20150425/95a86c1d/attachment.html>


More information about the cs615asa mailing list