JavaScript Regular Expressions

Book description

Leverage the power of regular expressions to create an engaging user experience

In Detail

Regular expressions are patterns or templates that allow you to define a set of rules in a natural yet vague way, giving you the ability to match and validate text. Therefore, they have been implemented in nearly every modern programming language. JavaScript's implementation allows us to perform complex tasks with a few lines of code using regular expressions to match and extract data out of text.

This book starts by exploring what a pattern actually is and how regular expressions express these patterns to match and manipulate user data. You then move on to learning about the use of character classes to define a wild character match, a digit match, and an alphanumeric match. You will then learn to manipulate text and shorten data in URLs, paths, markup, and data exchange, as well as other advanced Regex features.

Finally, you will work through real-world examples, both in the browser and on the server side using Node.js.

What You Will Learn

  • Structure your patterns and model different types of constraints
  • Clean and optimize code with Regex's processing power
  • Solve common use cases and situations
  • Extract different types of fields correctly
  • Embed regular expressions both in your UI and on the backend
  • Efficiently create guard clauses in your functions using patterns
  • Create blank capture groups to handle inconsistent data
  • Capture parts of a pattern in a group to display, replace, or use character sets to match characters

Publisher resources

Download Example Code

Table of contents

  1. JavaScript Regular Expressions
    1. Table of Contents
    2. JavaScript Regular Expressions
    3. Credits
    4. About the Authors
    5. About the Reviewers
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Getting Started with Regex
      1. It's all about patterns
        1. Analyzing a phone number
        2. Analyzing a simple log file
        3. Analyzing an XML file
      2. Regex in JavaScript
        1. The RegExp constructor
          1. Using pattern flags
        2. Using the rgx.test method
        3. Using the rgx.exec method
        4. The string object and regular expressions
          1. Using the String.replace method
          2. Using the method
          3. Using the String.match method
      3. Building our environment
        1. Handling a submitted form
        2. Resetting matches and errors
        3. Creating a regular expression
        4. Executing RegExp and extracting its matches
        5. Testing our application
      4. Summary
    9. 2. The Basics
      1. Defining vague matchers in Regex
        1. Matching a wild card character
        2. Matching digits
        3. Matching alphanumeric chars
        4. Negating alphanumeric chars and digits
      2. Defining ranges in Regex
        1. Defining a range
        2. Matching the dash character
        3. Defining negated ranges
      3. Defining multipliers in Regex
        1. Matching one or more occurrences
        2. Matching zero or one occurrence
        3. Matching zero or more occurrences
      4. Defining custom quantifiers
        1. Matching n or more occurrences
        2. Matching n to m occurrences
      5. Matching alternated options
      6. Creating a Regex for a telephone number
      7. Summary
    10. 3. Special Characters
      1. Nonvisual constraints
        1. Matching the beginning and end of an input
        2. Matching word boundaries
        3. Matching nonword boundaries
        4. Matching a whitespace character
      2. Defining nongreedy quantifiers
      3. Matching groups in Regex
        1. Grouping characters together to create a clause
          1. Capture and noncapture groups
            1. Matching non capture groups
        2. Matching lookahead groups
          1. Using a negative lookahead
      4. Summary
    11. 4. Regex in Practice
      1. Regular expressions and form validation
        1. Setting up the form
      2. Validating fields
        1. Matching a complete name
          1. Understanding the complete name Regex
        2. Matching an e-mail with Regex
          1. Understanding the e-mail Regex
        3. Matching a Twitter name
          1. Understanding the twitter username Regex
        4. Matching passwords
        5. Matching URLs
          1. Understanding the URL Regex
      3. Manipulating data
        1. Using the String.replace method
        2. Matching a description field
          1. Understanding the description Regex
          2. Explaining a Markdown example
      4. Summary
    12. 5. Node.js and Regex
      1. Setting up Node.js
      2. Getting started with our application
        1. Reading a file with Node.js
      3. The anatomy of an Apache log file
        1. Creating the Apache log Regex
          1. Creating a Regex for the time part
          2. Creating a Regex for the request information
          3. Creating a Regex for the status code and object size
          4. Creating a Regex for the referrer and the user agent
          5. Parsing each Apache log row
            1. Creating a JSON object for each row
            2. Display the JSON in a table
      4. Summary
    13. A. JavaScript Regex Cheat Sheet
      1. Character classes
        1. Literals
      2. Character sets
      3. Boundaries
      4. Grouping, alternation, and back reference
      5. Quantifiers
      6. JavaScript regular expressions methods
    14. Index

Product information

  • Title: JavaScript Regular Expressions
  • Author(s):
  • Release date: May 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781783282258