DevLab
Regex

Regex Basics: A Complete Beginner's Guide

Learn regular expressions from scratch. Understand patterns, metacharacters, and how to write your first regex.

What is a Regular Expression?

A regular expression (regex) is a sequence of characters that defines a search pattern. Think of it as a powerful "find" tool — but instead of searching for a specific word, you can search for a pattern of characters.

For example, instead of searching for exactly "hello123", you can write a regex that matches any combination of letters followed by any combination of digits. This makes regex incredibly useful for validating input, extracting data, and transforming text.

Regex is supported in virtually every programming language: JavaScript, Python, Java, Go, Ruby, PHP, and more. Once you learn regex, it works everywhere.

Your First Regex Pattern

The simplest regex is a literal string. The pattern hello matches the exact text "hello" anywhere in a string. But the real power comes from special characters called metacharacters.

Here are the most important metacharacters to start with:

  • . — Matches any single character (except newline)
  • * — Matches the previous character zero or more times
  • + — Matches the previous character one or more times
  • ? — Matches the previous character zero or one time (optional)
  • ^ — Matches the start of a string
  • $ — Matches the end of a string
  • \ — Escapes a metacharacter to treat it literally

Character Classes

Character classes let you match one character from a set of possibilities using square brackets [].

  • [aeiou] — Matches any vowel
  • [a-z] — Matches any lowercase letter
  • [A-Z0-9] — Matches any uppercase letter or digit
  • [^abc] — Matches any character except a, b, or c

There are also built-in shorthand character classes:

  • \d — Any digit (0-9)
  • \w — Any word character (letters, digits, underscore)
  • \s — Any whitespace (space, tab, newline)
  • \D, \W, \S — The opposite of the above

Quantifiers

Quantifiers control how many times a pattern element must match:

  • a{3} — Matches exactly 3 "a"s
  • a{2,4} — Matches 2 to 4 "a"s
  • a{2,} — Matches 2 or more "a"s

Your First Real-World Regex

Let's write a regex to validate a simple email address format: \w+@\w+\.\w+

  • \w+ — One or more word characters (the username)
  • @ — Literal @ symbol
  • \w+ — One or more word characters (the domain)
  • \. — Literal dot (escaped because . alone means "any character")
  • \w+ — One or more word characters (the TLD like "com")

This matches "user@example.com" but it's not perfect — email validation is a complex topic. Try it in the Regex Tester and experiment!

Practice: Common Beginner Patterns

The best way to learn regex is to practice. Try these patterns in the Regex Tester:

  • \d{4}-\d{2}-\d{2} — Matches dates like "2024-01-15"
  • https?://\S+ — Matches HTTP and HTTPS URLs
  • ^[A-Z] — Matches strings that start with a capital letter
  • \b\w{5}\b — Matches exactly 5-letter words

Frequently Asked Questions

What is a regular expression?

A regular expression (regex) is a sequence of characters that defines a search pattern. It is used in programming to match, find, and replace text based on patterns rather than exact strings.

Which programming languages support regex?

Nearly all modern programming languages support regex, including JavaScript, Python, Java, Go, Ruby, PHP, C#, Rust, and Swift. The core syntax is largely the same across languages, with minor dialect differences.

What does the dot (.) mean in regex?

The dot metacharacter matches any single character except a newline. For example, the pattern "h.t" matches "hat", "hit", "hot", and any other three-character string starting with h and ending with t.

How do I match a literal special character in regex?

Escape the special character with a backslash. For example, to match a literal period, use "\." instead of ".". This applies to all metacharacters: \*, \+, \?, \(, \), \[, \], \{, \}, \^, \$, \|, and \\.

Practice with these tools

More Learning Topics

RegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines