Learn Regular Expressions

Master regex patterns for text matching and manipulation.

Introduction

Regular expressions (regex) are patterns used to match character combinations in strings. They are powerful tools for text processing, validation, and manipulation.

Beginner Guide

Getting Started with Regular Expressions

#

Basic Patterns

  • a - Matches the character 'a'
  • abc - Matches the string 'abc'
  • a|b - Matches 'a' or 'b'
  • . - Matches any single character except newline
  • ^a - Matches 'a' at the start of a string
  • a$ - Matches 'a' at the end of a string
  • #

    Character Classes

  • [abc] - Matches 'a', 'b', or 'c'
  • [^abc] - Matches anything except 'a', 'b', or 'c'
  • [a-z] - Matches any lowercase letter
  • [A-Z] - Matches any uppercase letter
  • [0-9] - Matches any digit
  • #

    Quantifiers

  • a* - Matches zero or more 'a's
  • a+ - Matches one or more 'a's
  • a? - Matches zero or one 'a'
  • a{3} - Matches exactly 3 'a's
  • a{2,4} - Matches 2 to 4 'a's
  • #

    Common Examples

    ``javascript // Email validation /^[\w.-]+@[\w.-]+\.\w+$/

    // Phone number validation /^\+?[1-9]\d{1,14}$/

    // URL validation /https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w.,@?^=%&:/~+#-]*$/ ``

    Advanced Guide

    Advanced Regex Concepts

    #

    Grouping and Capturing

  • (abc) - Creates a capturing group
  • (?:abc) - Creates a non-capturing group
  • (?abc) - Creates a named capturing group
  • #

    Lookaround

  • (?=abc) - Positive lookahead (matches if followed by 'abc')
  • (?!abc) - Negative lookahead (matches if not followed by 'abc')
  • (?<=abc) - Positive lookbehind (matches if preceded by 'abc')
  • (? - Negative lookbehind (matches if not preceded by 'abc')
  • #

    Backreferences

  • \1 - References the first capturing group
  • \k - References a named capturing group
  • #

    Flags

  • g - Global search (find all matches)
  • i - Case insensitive
  • m - Multiline mode
  • s - Dotall mode (dot matches newline)
  • u - Unicode mode
  • #

    Performance Tips

  • Avoid excessive backtracking
  • Use non-capturing groups when you don't need to capture
  • Be specific instead of using greedy quantifiers
  • Compile regex patterns for repeated use
  • Common Errors

    • Forgetting to escape special characters like . * + ?
    • Using greedy quantifiers when lazy ones are needed
    • Not anchoring patterns with ^ and $ when needed
    • Overusing lookahead/lookbehind unnecessarily
    • Case sensitivity issues
    • Not handling multiline text correctly
    • Using capturing groups when non-capturing would suffice
    • Performance issues with complex patterns

    FAQ

    What is regex used for?

    Regex is used for text validation, searching, replacing, and parsing.

    What does the dot (.) match?

    The dot matches any single character except newline (unless using the s flag).

    What is the difference between * and +?

    * matches zero or more occurrences, + matches one or more occurrences.

    How do I match a literal dot?

    Escape it with a backslash: \.

    What is backtracking?

    Backtracking is when the regex engine tries different paths to find a match, which can cause performance issues.

    What are lookahead and lookbehind?

    Lookahead and lookbehind are zero-width assertions that match a position based on what follows or precedes it.

    Which flag makes regex case-insensitive?

    The i flag makes the regex case-insensitive.

    How do I extract matched groups?

    Use capturing groups with parentheses and access them via match indices or named groups.

    Ready to Practice?

    Try our tools to apply what you have learned in real-time.

    DevKitFlow - Free Online Developer Tools