SmartDigitalTips
Developer

Regex for Beginners: A Plain-English Guide to Regular Expressions

By SmartDigitalTips Team • June 16, 2026

What Are Regular Expressions?

A regular expression (regex) is a sequence of characters that defines a search pattern. You use regex to find, match, or replace text that follows a specific pattern — like "find all email addresses" or "extract all phone numbers" or "replace all dates formatted as MM/DD/YYYY."

Regex looks intimidating at first: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$/. But it's built from simple rules that you can learn one at a time. Our free Regex Tester lets you experiment with patterns and see matches in real-time — the best way to learn is to try things.

The Building Blocks of Regex

Literal Characters

The simplest regex is just literal text. The pattern cat matches the word "cat" anywhere in a string. Case-sensitive by default: cat matches "cat" but not "Cat."

The Dot (.) — Any Character

A dot matches any single character except a newline. c.t matches "cat", "cut", "c4t", "c!t" — anything where c and t are separated by exactly one character.

Character Classes [ ]

Square brackets match any one character from a set. [aeiou] matches any single vowel. [0-9] matches any digit. [a-z] matches any lowercase letter. [a-zA-Z] matches any letter.

Add ^ inside brackets to negate: [^0-9] matches any character that is NOT a digit.

Quantifiers — How Many Times

  • * — zero or more times. ab*c matches "ac", "abc", "abbc", "abbbc"
  • + — one or more times. ab+c matches "abc", "abbc" but NOT "ac"
  • ? — zero or one time (optional). colou?r matches both "color" and "colour"
  • {n} — exactly n times. [0-9]{4} matches exactly 4 digits
  • {n,m} — between n and m times. [a-z]{2,5} matches 2 to 5 lowercase letters

Anchors — Position in String

  • ^ — start of string (or start of line in multiline mode)
  • $ — end of string

^hello matches "hello world" but not "say hello." world$ matches "hello world" but not "worldwide."

Shorthand Character Classes

  • d — any digit (same as [0-9])
  • D — any non-digit
  • w — any word character (letters, digits, underscore) [a-zA-Z0-9_]
  • W — any non-word character
  • s — any whitespace (space, tab, newline)
  • S — any non-whitespace

5 Practical Regex Patterns You Can Use Today

1. Validate an Email Address

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$

Matches: user@example.com, first.last@company.co.uk

2. Match a US Phone Number

^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$

Matches: (555) 123-4567, 555.123.4567, 5551234567

3. Find All URLs in Text

https?://[^s]+

Matches any URL starting with http:// or https://

4. Validate a Strong Password

^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[@$!%*?&])[A-Za-zd@$!%*?&]{8,}$

Requires: min 8 chars, at least one lowercase, uppercase, digit, and special character

5. Match a Date (YYYY-MM-DD)

^d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]d|3[01])$

Matches: 2026-06-23 but not 2026-13-01 or 2026-06-32

Practice Regex Right Now

The fastest way to learn regex is to experiment. Our free Regex Tester shows you real-time matches as you type your pattern. Paste some sample text, write a pattern, and watch it highlight matches instantly. No setup, no software — just open and start testing.

When Should You Use Regex?

  • Validating form inputs (email, phone, zip code)
  • Finding and replacing patterns in large text files
  • Extracting structured data from unstructured text
  • Log file analysis
  • Search-and-replace in code editors (VS Code, Sublime Text both support regex)

When you don't need regex: for simple fixed-string searches, string comparison (===), or when a dedicated parsing library exists (don't parse HTML with regex — use a proper DOM parser).

Looking for free digital tools?

SmartDigitalTips offers 50+ completely free tools for images, PDFs, text, and developers that run 100% locally in your browser.

Explore all tools