Tuesday, July 14, 2015

Book Review: JavaScript Regular Expressions

Loiane Groner's and Gabriel Manricks's book JavaScript Regular Expressions (Packt Publishing, May 2015) has the subtitle, "Leverage the power of regular expressions to create an engaging user experience." The book is relatively short with five chapters a preface, and an appendix spanning approximately 90 substantive pages.

Preface

The preface provides a sentence of two describing each of JavaScript Regular Expressions's five chapters and its appendix. The preface explains that a text editor and web browser are all that are needed for most of the book's examples, but states that Node.js is required for Chapter 5's examples. The preface also states that the book is intended "for JavaScript developers who work with any type of user entry data" and "for JavaScript programmers who possess basic to intermediate skills in JavaScript regular expressions, and want to learn about these for the first time or sharpen their skills to become experts."

Chapter 1: Getting Started with Regex

The first chapter of JavaScript Regular Expressions introduces regular expressions (regex) by covering the idea behind regular expressions from a conceptual perspective. It then moves onto trying out regular expressions in JavaScript and specifically mentions using a browser's JavaScript console to try them out. The RegExp constructor is introduced.

A section in Chapter 1 introduces pattern flags as ignore case (i), multiline (m), and global match (g). Another section describes using RegExp functions test() and exec() and using String functions replace(), search, and match.

Chapter 1 concludes with a simple web form example for testing regular expressions examples that is built on HTML, JavaScript, Cascading Style Sheets, Bootstrap, and jQuery. This example does a nice job of illustrating the basic functions of RegExp discussed earlier in the chapter and at the same time provides a handy tool for testing regular expressions.

Chapter 2: The Basics

JavaScript Regular Expressions's second chapter introduces "vague matchers" and multipliers. The section on "vague matchers" introduces the regular expressions metadata/character characters such as dot (period or "wildcard character"), digit (\d), word (\w), and negated forms (\D and \W). It also explains and demonstrates specifying ranges, escaping hyphen, and negating ranges.

The "multipliers" section of the second chapter introduces the standard multipliers: + (one or more), * (zero or more), and ? (zero or one). The chapter then describes and illustrates more customizable multipliers using curly brace syntax. Chapter 2 also introduces the regular expression pipe (alternation).

Chapter 3: Special Characters

The third chapter covers "more advanced techniques" associated with regular expressions: boundaries, nongreedy quantifiers, and groups. The chapter introduces "nonvisual constraints" such as the anchors caret (^) for indicating the start of a line and dollar sign ($) for indicating the end of a line. Other early chapter discussions involve matching word boundaries (\b), matching non-word boundaries (\B), and matching whitespace (\s). Chapter 3's section on nongreedy quantifiers explains how to use the question mark (?) to specify "ungreedy quantifiers."

Chapter 3's examples for applying groups move back to using the web browser's JavaScript console instead of the example HTML application presented in the initial chapter. This section demonstrates applying groups in regular expressions in JavaScript code and introduces capture groups, noncapture groups (?:), back references, lookahead groups (?=), and negative lookahead (?!).

Chapter 4: Regex in Practice

JavaScript Regular Expression's fourth chapter provides "some actual use cases" to illustrate "some common design patterns" in crafting regular expressions. The chapter provides an example of building a form with JavaScript code (including regular expression handling) used for parsing and validating the form input. The example is constructed step-by-step with text explanations of each code listing. The specific portions of the form that are illustrated are name validation, e-mail validation, Twitter username validation, password validation, URL validation, text validation, and text manipulation (including a Markdown example).

Chapter 5: Node.js and Regex

The final chapter of JavaScript Regular Expressions explains "how to implement a simple Node.js application that reads a log file and parses it using a regular expression." The chapter begins by explaining how to download Node.js and recommending that those new to Node.js review the tutorials at https://nodejs.org/. After discussing how to verify that Node.js is installed correctly, the chapter looks at "reading a file with Node.js." It describes configuring Node.js to work with the file system and explains the anatomy of an Apache log file.

Chapter 5 provides a detailed discussion regarding the construction of regular expressions for parsing the Apache log file. An interesting twist presented in the chapter is representing each Apache log row in JavaScript Object Notation (JSON). There is then an example of presenting this JSON in an HTML table.

Appendix: JavaScript Regex Cheat Sheet

JavaScript Regular Expressions's appendix is just over 6 pages that consist of tables presenting columns with patterns, descriptions, and examples for various types of regular expressions including character classes, character sets, literals, boundaries, alternation, grouping, back reference, and quantifiers. The final table in the appendix is in the section "JavaScript regular expressions methods" and this table displays patterns, descriptions, and examples of JavaScript methods (mostly on String and RegExp) that are "used to match or test a regular expression."

General Observations

  • JavaScript Regular Expressions provides a nice introduction to applying regular expressions in JavaScript and is a quick read with fewer than 100 pages.
    • The significant general coverage of regular expressions is likely to be useful to anyone new to regular expressions in any programming language, but the book does discuss integrating regular expressions with JavaScript code and using JavaScript's particular flavor of regular expressions.
    • Only basic familiarity with JavaScript is required to benefit from the book.
    • JavaScript developers who are already comfortable with regular expressions will probably find less new material in the book, though I did learn some things from reading it even though I'm fairly comfortable with regular expressions and JavaScript.
  • The electronic version (PDF) of JavaScript Regular Expressions that I reviewed includes color screen snapshots.
  • Code listings in JavaScript Regular Expressions are black font on white background with no line numbers and no color syntax.
  • The examples in JavaScript Regular Expressions are generally short and easy to follow and are generally described in detail. I particularly liked the example of the regular expression testing web page and the example of building an HTML form with regular expression based validation.
  • The appendix of JavaScript Regular Expressions provides a concise reference of the most important aspects of regular expressions.

Conclusion

JavaScript Regular Expressions packs a lot into a relatively small book that is easy to read. The examples are basic and of small enough size to be easy to digest. The book contains lots of practical advice and would be especially beneficial to someone with basic familiarity with JavaScript but little or no knowledge of regular expressions. The book could also benefit someone wanting to learn basics of regular expressions even in languages other than JavaScript as the book covers regular expressions in a general way more often than it covers regular expressions in a JavaScript-specific way.

No comments: