Learn Regex: A Beginner’s Guide

Here’s a great article from SitePoint

In this guide, you’ll learn regex, or regular expression syntax. By the end, you’ll be able to apply regex solutions in most scenarios that call for it in your web development work.

Regular expressions have many uses cases, which include:

  • form input validation
  • web scraping
  • search and replace
  • filtering for information in massive text files such as logs

Regular expressions, or regex as they’re commonly called, look complicated and intimidating for new users. Take a look at this example:

/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$/

It just look like garbled text. But don’t despair, there’s method behind this madness.

xkcd.com/208

Credit: xkcd

I’ll show you how to master regular expressions in no time. First, let’s clarify the terminology used in this guide:

  • pattern: regular expression pattern
  • string: test string used to match the pattern
  • digit: 0-9
  • letter: a-z, A-Z
  • symbol: !$%^&*()_+|~-=`{}[]:”;'<>?,./
  • space: single white space, tab
  • character: refers to a letter, digit or symbol

Basics

To learn regex quickly with this guide, visit Regex101, where you can build regex patterns and test them against strings (text) that you supply.

When you open the site, you’ll need to select the JavaScript flavor, as that’s what we’ll be using for this guide. (Regex syntax is mostly the same for all languages, but there are some minor differences.)

Next, you need to disable the global and multi line flags in Regex101. We’ll cover them in the next section. For now, we’ll look at the simplest form of regular expression we can build. Input the following:

Continue reading
Learn Regex: A Beginner’s Guide
on SitePoint.

Source link