SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Book "O'Reilly - Mastering Regular Expressions" usefulness for JavaScript?

    Hi-
    I had run in to some JavaScript code I wish to understand which used regular expressions, so I attempted to learn regular expressions as used in JavaScript from wed based tutorials. I found these tutorials not to my liking. They fail to dissect the regular expressions sufficiently for me to learn them. So I was quite happy when I obtained a book called "O'Reilly - Mastering Regular Expressions second edition" since when I started to read the first chapter I began to get the intense dissection of the regular expressions I had desired. However when I tried one of the expressions found in the first chapter in a JavaScript regular expression online evaluation tool it yielded a result that was not the same as what the book was saying would be the result. Perhaps I should quote the book to better explain exactly what I am talking about so from the first chapter of the book here is my quote.

    ^( From|Subject|Date):
    The alternation is constrained by the parentheses, so literally, this regex means
    ˘match the start of the line, then one of !From", !Subject", or !Date", and then match
    !: ".÷ Effectively, it matches:
    1) start-of-line, followed by F r o m, followed by `: '
    or 2) start-of-line, followed by S u b j e c t, followed by `: '
    or 3) start-of-line, followed by D a t e, followed by `: '
    Putting it less literally, it matches lines beginning with `From: ', `Subject: ', or
    `Date: ', which is quite useful for listing the messages in an email ňle.
    The online evaluation tool I was using can be found here. And supports a number of languages not just JavaScript. Switching the language switch to JavaScript and testing some test text in the text box I only got the word "From" as outlined in the quote above. Let me specify my test text. Here it is.
    orFrom: elvis@tabloid.org (The King)From:
    Subject: be seein' ya around Subject:
    Ignoring Differences in Capitalization
    Date: Thu, 22 Aug Date: 2002 11:04:13 Date:
    Ignoring Differences in Capitalization Date:
    From: The Prez president@whitehouse.gov
    Ignoring Differences Subject: in Capitalization
    Date: Tue, 27 Aug 2002 8:36:24
    Subject: now, about your vote
    I was however able to get this online regular expression evaluation tool to yield the exact result outlined in the book quote above. To do this it was necessary to switch the language selection switch to "Ruby" and select the global checkbox as well.

    So what does this mean. Can I use my book effectively to help me learn regular expressions as used in JavaScript. Can you suggest any other sources of intense regular expression dissection the way it would be used in JavaScript. Obtaining this book took some effort which I would not want to waste. So please give me your best advice.

    Sincerely
    Marc

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, it matches the start of the string normally, unless you are using the multi-line "m" modifier where "^" indicates the start of the line as well as the start of the string. If you want to match all of them, you'll need the global "g" modifier as well.

    Older browsers don't support the multi-line modifier and I don't think I've ever used it in a real script.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    There are a few aspects of regular expressions that the book covers which are not supported by Javascript but on the whole the information in that book on regular expressions is excellent and most does apply to Javascript. Now if only the author actually understood HTML he might have been tempted to include Javascript in the book along with the other languages covered.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ^( From|Subject|Date):
    The alternation is constrained by the parentheses, so literally, this regex means
    &#244;match the start of the line, then one of !From", !Subject", or !Date", and then match
    !: ".&#246; Effectively, it matches:
    1) start-of-line, followed by F r o m, followed by `: '
    or 2) start-of-line, followed by S u b j e c t, followed by `: '
    or 3) start-of-line, followed by D a t e, followed by `: '
    Putting it less literally, it matches lines beginning with `From: ', `Subject: ', or
    `Date: ', which is quite useful for listing the messages in an email &#229;le.
    Whoa. Turn off the hieroglyphics.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •