ProgrammingTutorial

Ultimate Regex Tutorial - Regular Expressions Explained for Developers

TT
TopicTrick
Ultimate Regex Tutorial - Regular Expressions Explained for Developers

Ultimate Regex Tutorial - Regular Expressions Explained

Regular expressions (regex) are one of the most powerful — and feared — tools in a developer's toolkit. Once you understand them, you'll wonder how you ever lived without them.

This complete regex tutorial will take you from zero to writing complex patterns with confidence.


What is Regex?

A regular expression is a sequence of characters that defines a search pattern. Regex engines can search, match, extract, and replace text based on these patterns.

Regex is supported in virtually every programming language and is essential for:

  • Form validation: Email addresses, phone numbers, strong passwords.
  • Data extraction: Parsing server logs, scraping structured text.
  • Search and replace: Finding complex patterns and transforming text.
  • String manipulation: Splitting and formatting text programmatically.

Basic Syntax & Pattern Matching

At its core, a regex is just a string of characters representing a rule. The simplest rule is a literal word. If your regex is /hello/, it will find the exact word "hello".

However, regex gets truly powerful when we use flags — special characters that change how the search behaves.

javascript
1// JavaScript regex basics 2const pattern = /hello/; // Regex literal wrapped in /slashes/ 3 4// Test if a string matches 5console.log(pattern.test("hello world")); // true 6console.log(pattern.test("Hello World")); // false (case-sensitive by default) 7 8// Case-insensitive 'i' flag 9const caseInsensitive = /hello/i; 10console.log(caseInsensitive.test("Hello World")); // true 11 12// Global 'g' flag - find all matches, not just the first one 13const globalPattern = /the/g; 14const text = "the cat sat on the mat"; 15console.log(text.match(globalPattern)); // ["the", "the"]

Special Characters in Regex

CharacterExampleDescription
./h.t/Matches any single character except a newline (e.g., "hat", "hot").
^/^hello/Ensures the pattern only matches at the beginning of a string.
$/world$/Ensures the pattern only matches at the end of a string.
\d/\d+/Matches any single digit character (0-9).
\w/\w+/Matches any alphanumeric character or underscore (word characters).
\s/\s+/Matches any whitespace character (space, tab, newline).
\D/\D+/Opposite of \d. Matches any non-digit character.

Character Classes & Quantifiers

What if you don't know exactly what word you're looking for, but you know it must be a 5-digit number? This is where Character Classes and Quantifiers come in.

javascript
1// Character classes - match any ONE character from a set 2const vowels = /[aeiou]/g; 3"hello world".match(vowels); // ["e", "o", "o"] 4 5// Negation with ^ inside brackets 6const noVowels = /[^aeiou\s]/g; 7"hello".match(noVowels); // ["h", "l", "l"] 8 9// Ranges in character classes 10const lowercase = /[a-z]/; 11const digit = /[0-9]/; 12const hexColor = /[0-9a-fA-F]/;

Quantifiers define how many times a character (or group) must appear:

  • * : 0 or more matches
  • + : 1 or more matches
  • ? : 0 or 1 match (optional)
  • {3} : Exactly 3 matches
  • {2,5} : Between 2 to 5 matches

Groups & Capturing

Sometimes you don't just want to find a match; you want to extract a specific piece of it. Capturing groups (using parentheses) let you "save" parts of your match into memory.

javascript
1// Capturing group - extracts Year, Month, and Day 2const datePattern = /(\d{4})-(\d{2})-(\d{2})/; 3const dateString = "Today is 2026-03-16."; 4const match = dateString.match(datePattern); 5 6if (match) { 7 console.log(match[0]); // "2026-03-16" - The full match 8 console.log(match[1]); // "2026" - Group 1 (Year) 9 console.log(match[2]); // "03" - Group 2 (Month) 10 console.log(match[3]); // "16" - Group 3 (Day) 11}

Alternation (OR Logic)

Use the pipe `\|` character to match either one thing or another. E.g., `/cat\|dog/` will match either 'cat' or 'dog'.


    Common Regex Patterns

    Here are the most useful, real-world patterns that every developer should know (and save):

    javascript
    1// Email Validation 2const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; 3 4// URL Matching 5const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)/; 6 7// Phone Number (US Format) 8const phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/; 9 10// Hex Color Codes 11const hexColorRegex = /#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/g;

    Python Regex Examples

    While JavaScript has regex built into the core language, Python uses the powerful re module. The syntax inside the pattern is nearly identical.

    python
    1import re 2 3text = "Contact support at user@example.com today." 4 5# Find first match 6match = re.search(r'[\w.]+@[\w.]+', text) 7if match: 8 print(match.group()) # Outputs: user@example.com 9 10# Replace (substitute) matches 11clean_text = re.sub(r'\d+', 'NUM', "There are 3 cats and 5 dogs") 12# Outputs: "There are NUM cats and NUM dogs"

    Conclusion

    Regular Expressions might look like gibberish at first, but with practice, they become an incredibly logical and powerful tool. The best way to learn is by testing real strings safely before putting them in your code.