JavaScript Regular Expressions
By Loiane Groner and Gabriel Manricks
3/5
()
About this ebook
- Build and use patterns in JavaScript to solve real-world problems
- Simplify complex validation processes by mastering the pattern language in Regex
- A step-by-step guide along with a lot of real-world examples to manipulate text and crunch data, in URLs, paths, markup, and so on
This book is ideal for JavaScript developers and programmers who work with any type of user entry data and want sharpen their skills to become experts.
Loiane Groner
Loiane Groner lives in São Paulo, Brazil, and has over 8 years of software development experience. While at university, Loiane demonstrated great interest in IT. She worked as a Teaching Assistant for 2 years and 6 months in the Algorithms, Data Structures, and Computing Theory Department. She represented her university at the ACM International Collegiate Programming Contest - Brazilian Finals (South America Regionals) and also worked as Student Delegate of SBC (Brazilian Computing Society) for 2 years. She won a merit award in her senior year for being one of the top 3 students with the best GPAs in the Computer Science department, and she also graduated with honors.She has already in her career worked at multinational companies such as IBM. Her areas of expertise include Java SE and Java EE and also Sencha technologies (Ext JS and Sencha Touch). Nowadays, she is working as a Software Development Manager at a financial institution, where she manages overseas solutions. She also works as an independent Sencha consultant and coach.Loiane is also the author of Ext JS 4 First Look and Mastering Ext JS, both published by Packt.She is passionate about Sencha and Java, and she is the CampinasJUG (Campinas Java Users Group) leader and ESJUG (Espirito Santo Java Users Group) coordinator, both of which are Brazilian JUGs.Loiane also contributes to the software development community through her blogs: http://loianegroner.com (English) and http://loiane.com (Portuguese-BR), where she writes about her IT career, Ext JS, Sencha Touch, Spring Framework, and she also discusses general development notes and publishes screencasts.
Read more from Loiane Groner
Learning JavaScript Data Structures and Algorithms Rating: 5 out of 5 stars5/5Learning JavaScript Data Structures and Algorithms - Second Edition Rating: 0 out of 5 stars0 ratingsMastering Ext JS - Second Edition Rating: 0 out of 5 stars0 ratings
Related to JavaScript Regular Expressions
Related ebooks
Mastering JavaScript Design Patterns - Second Edition Rating: 5 out of 5 stars5/5Node.js By Example Rating: 2 out of 5 stars2/5Getting Started with React Rating: 0 out of 5 stars0 ratingsMastering JavaScript Object-Oriented Programming Rating: 0 out of 5 stars0 ratingsMastering JavaScript Rating: 4 out of 5 stars4/5Getting Started with React Native Rating: 4 out of 5 stars4/5Clean Code in JavaScript: Develop reliable, maintainable, and robust JavaScript Rating: 5 out of 5 stars5/5JavaScript Unlocked Rating: 5 out of 5 stars5/5Mastering JavaScript Design Patterns Rating: 4 out of 5 stars4/5Express Web Application Development Rating: 3 out of 5 stars3/5JavaScript and JSON Essentials Rating: 5 out of 5 stars5/5Node.js High Performance Rating: 0 out of 5 stars0 ratingsJavaScript Projects for Kids Rating: 0 out of 5 stars0 ratingsTypeScript Essentials Rating: 4 out of 5 stars4/5ASP.NET Core 3 and React: Hands-On full stack web development using ASP.NET Core, React, and TypeScript 3 Rating: 0 out of 5 stars0 ratingsGo Programming Blueprints Rating: 0 out of 5 stars0 ratingsDeploying Node.js Rating: 5 out of 5 stars5/5Test-Driven JavaScript Development Rating: 0 out of 5 stars0 ratingsLearning jQuery 3 - Fifth Edition Rating: 0 out of 5 stars0 ratingsLearning Single-page Web Application Development Rating: 0 out of 5 stars0 ratingsAngularJS Deployment Essentials Rating: 0 out of 5 stars0 ratingsLearning Swift Rating: 5 out of 5 stars5/5Reactive Programming for .NET Developers Rating: 0 out of 5 stars0 ratingsJavaScript for Gurus: Use JavaScript programming features, techniques and modules to solve everyday problems Rating: 0 out of 5 stars0 ratingsReact Design Patterns and Best Practices Rating: 0 out of 5 stars0 ratingsReactJS for Jobseekers: The Only Guide You Need to Learn React and Crack Interviews (English Edition) Rating: 0 out of 5 stars0 ratings50 Recipes for Programming Node.js Rating: 3 out of 5 stars3/5Node.js Design Patterns - Second Edition Rating: 4 out of 5 stars4/5Vue.js 2 Cookbook Rating: 0 out of 5 stars0 ratingsWeb Developer's Reference Guide Rating: 0 out of 5 stars0 ratings
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5A Slackers Guide to Coding with Python: Ultimate Beginners Guide to Learning Python Quick Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsThe Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5TensorFlow in 1 Day: Make your own Neural Network Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5C# Programming from Zero to Proficiency (Beginner): C# from Zero to Proficiency, #2 Rating: 0 out of 5 stars0 ratingsProgramming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratings
Reviews for JavaScript Regular Expressions
3 ratings0 reviews
Book preview
JavaScript Regular Expressions - Loiane Groner
Table of Contents
JavaScript Regular Expressions
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Getting Started with Regex
It's all about patterns
Analyzing a phone number
Analyzing a simple log file
Analyzing an XML file
Regex in JavaScript
The RegExp constructor
Using pattern flags
Using the rgx.test method
Using the rgx.exec method
The string object and regular expressions
Using the String.replace method
Using the String.search method
Using the String.match method
Building our environment
Handling a submitted form
Resetting matches and errors
Creating a regular expression
Executing RegExp and extracting its matches
Testing our application
Summary
2. The Basics
Defining vague matchers in Regex
Matching a wild card character
Matching digits
Matching alphanumeric chars
Negating alphanumeric chars and digits
Defining ranges in Regex
Defining a range
Matching the dash character
Defining negated ranges
Defining multipliers in Regex
Matching one or more occurrences
Matching zero or one occurrence
Matching zero or more occurrences
Defining custom quantifiers
Matching n or more occurrences
Matching n to m occurrences
Matching alternated options
Creating a Regex for a telephone number
Summary
3. Special Characters
Nonvisual constraints
Matching the beginning and end of an input
Matching word boundaries
Matching nonword boundaries
Matching a whitespace character
Defining nongreedy quantifiers
Matching groups in Regex
Grouping characters together to create a clause
Capture and noncapture groups
Matching non capture groups
Matching lookahead groups
Using a negative lookahead
Summary
4. Regex in Practice
Regular expressions and form validation
Setting up the form
Validating fields
Matching a complete name
Understanding the complete name Regex
Matching an e-mail with Regex
Understanding the e-mail Regex
Matching a Twitter name
Understanding the twitter username Regex
Matching passwords
Matching URLs
Understanding the URL Regex
Manipulating data
Using the String.replace method
Matching a description field
Understanding the description Regex
Explaining a Markdown example
Summary
5. Node.js and Regex
Setting up Node.js
Getting started with our application
Reading a file with Node.js
The anatomy of an Apache log file
Creating the Apache log Regex
Creating a Regex for the time part
Creating a Regex for the request information
Creating a Regex for the status code and object size
Creating a Regex for the referrer and the user agent
Parsing each Apache log row
Creating a JSON object for each row
Display the JSON in a table
Summary
A. JavaScript Regex Cheat Sheet
Character classes
Literals
Character sets
Boundaries
Grouping, alternation, and back reference
Quantifiers
JavaScript regular expressions methods
Index
JavaScript Regular Expressions
JavaScript Regular Expressions
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: May 2015
Production reference: 1250515
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78328-225-8
www.packtpub.com
Credits
Authors
Loiane Groner
Gabriel Manricks
Reviewers
Andrea Barisone
Florian Bruniaux
Robert K Casto
Julio Freitas
Juri Strumpflohner
Commissioning Editor
Kunal Parikh
Acquisition Editor
Subho Gupta
Content Development Editor
Rohit Kumar Singh
Technical Editors
Bhupesh Kothari
Mrunmayee Patil
Copy Editor
Sonia Michelle Cheema
Project Coordinator
Mary Alex
Proofreaders
Stephen Copestake
Safis Editing
Indexer
Monica Ajmera Mehta
Graphics
Sheetal Aute
Production Coordinator
Conidon Miranda
Cover Work
Conidon Miranda
About the Authors
Loiane Groner has over 9 years of software development experience. In her university, she demonstrated a great deal of interest in IT. She worked as a teacher's assistant for 2.5 years for algorithms, data structures, and computing theory. She represented her university at the ACM International Collegiate Programming Contest – Brazilian Finals (South America Regionals), and she also worked as the student delegate of the SBC (Brazilian Computing Society) for 2 years. Loiane won a merit award in her senior year for being one of the top three students in her course. She had one of the highest GPAs in the computer science department, and also graduated with honors.
She has already worked at multinational companies, such as IBM. Her areas of expertise include Java SE and Java EE and also Sencha technologies (such as Ext JS and Sencha Touch). Nowadays, Loiane is working as a software development manager at a financial institution where she manages overseas solutions. She also works as an independent Sencha consultant and coach.
Loiane is also the author of Ext JS 4 First Look, Mastering Ext JS (first and second editions), Sencha Architect App Development, and Learning JavaScript Data Structure and Algorithms, all published by Packt Publishing.
She is passionate about Sencha and Java, and she is the CampinasJUG (Campinas Java Users Group) leader and an ESJUG (Espirito Santo Java Users Group) coordinator, both of which are Brazilian JUGs.
Loiane also contributes to the software development community through her blogs, which can be found at http://loianegroner.com (the English version) and http://loiane.com (the Portuguese-BR version), where she writes about IT careers, Ext JS, Sencha Touch, PhoneGap, Spring Framework, Java, and general development notes and also publishes screencasts.
If you want to keep in touch with her, you can find her on Facebook (https://www.facebook.com/loianegroner) and on Twitter (@loiane).
I would like to thank my parents for educating, guiding, and advising me through all these years and for helping me become a better human being and professional. A very special thank you to my husband for being patient and supportive and for giving me encouragement throughout.
I would like to thank Packt Publishing for this amazing opportunity to write books about the topics I really love! I'd like to thank all the people involved in the process of creating, reviewing, and publishing these books!
I would also like to thank the readers of this book and the other books I have written for their support and feedback. Your feedback is very valuable in helping me improve as an author and a professional. Thank you very much!
Gabriel Manricks is a full-stack software and web developer, and a writer. He is the CTO at CoinSimple and a staff writer at Nettuts+, where he enjoys learning as well as teaching others.