JSONHint

Preparing your workspace…

Understanding Regular Expressions (Regex)

A complete beginner to intermediate guide to understanding Regex syntax, writing efficient patterns, and validating text strings in modern development.

JH
JSONHint
By JSONHint · Practical guide

What is a Regular Expression?

A Regular Expression (often abbreviated as regex or regexp) is a sequence of characters that specifies a search pattern in text. Developed originally in theoretical computer science to describe formal languages, regex is now an indispensable tool implemented in nearly every modern programming language (JavaScript, Python, Go, Java).

Developers use regex to perform highly complex "find and replace" operations, validate user input (such as making sure an email address is actually an email address), and extract specific data from massive blocks of log files.

Decoding the Syntax

At first glance, a regular expression looks like complete gibberish (e.g., ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$). However, once you break down the special characters, it becomes a heavily logical puzzle.

Anchors

  • ^ matches the absolute beginning of a line or string.
  • $ matches the absolute end of a line or string.

Character Classes

  • \d matches any single digit (0-9). Equivalent to [0-9].
  • \w matches any "word" character (a-z, A-Z, 0-9, and the underscore).
  • \s matches any whitespace character (space, tab, newline).
  • . matches ANY single character (except a newline).
  • [abc] matches exactly "a", "b", or "c".

Quantifiers (How many times?)

  • * matches zero or more occurrences of the preceding element.
  • + matches one or more occurrences. (Must appear at least once).
  • ? matches zero or one occurrence. (Makes the preceding element optional).
  • {3} matches exactly 3 times.
  • {3,5} matches between 3 and 5 times.

Practical Regex Examples

Finding a standard ZIP Code (US)

^\d{5}$

Breakdown: ^ starts the string. \d looks for a digit. {5} ensures there are exactly five digits. $ ends the string. This prevents it from matching "123456" as well.

Finding a HTTP/HTTPS URL

^https?:\/\/.*

Breakdown: http matches the literal text. s? makes the "s" optional (matching both http and https). :\/\/ escapes the literal colon and slashes so they aren't treated as regex operators. .* then matches any character endlessly until the string ends.

Common Regex Pitfalls: Catastrophic Backtracking

If a regular expression is poorly written, particularly when using multiple nested quantifiers like (a+)+, the regex engine may attempt to search every possible permutation of a string if it fails to find an exact match at the end. This is known as "Catastrophic Backtracking" and can instantly lock up your server's CPU and cause a Denial of Service (ReDoS) attack.

Practice your Regex Live

The absolute best way to learn regular expressions is to experiment with them safely. Use our real-time Regex tester to type patterns and instantly see what they capture.

Open Regex Tester Tool