DevLab
Regex

Regex Special Characters: Complete Reference

Master every regex metacharacter and special character with examples and interactive tests.

Metacharacters Overview

In regex, certain characters have special meanings. These are called metacharacters. To match them literally, you must escape them with a backslash: \. matches a literal period.

The metacharacters are: . ^ $ * + ? { } [ ] \ | ( )

The Dot: Match Any Character

The dot . matches any single character except a newline. The pattern c.t matches "cat", "cot", "cut", "c!t", but not "ct" (nothing between c and t) or "coat" (two characters).

To match an actual period, escape it: \.

Anchors: ^ and $

^ matches the start of a string. ^Hello matches "Hello World" but not "Say Hello".

$ matches the end of a string. end$ matches "the end" but not "ending".

Combined: ^\d+$ matches strings that are entirely digits — useful for validating numeric input.

Word Boundaries: \b

\b matches the position between a word character and a non-word character. The pattern \bcat\b matches "cat" and "the cat sat" but NOT "catch" or "concatenate".

This is very useful when you want to match whole words only.

Alternation: |

The pipe character | means "or". The pattern cat|dog matches either "cat" or "dog". When combined with groups: (cat|dog)s matches "cats" or "dogs".

Escape Sequences

Special escape sequences represent non-printable or commonly used character groups:

  • \n — Newline
  • \t — Tab
  • \r — Carriage return
  • \d — Digit [0-9]
  • \D — Non-digit [^0-9]
  • \w — Word character [a-zA-Z0-9_]
  • \W — Non-word character
  • \s — Whitespace [ \t\n\r\f\v]
  • \S — Non-whitespace

Greedy vs. Lazy Matching

By default, quantifiers are greedy — they match as much as possible. The pattern <.+> applied to <b>text</b> matches the entire string <b>text</b>, not just <b>.

Add ? after a quantifier to make it lazy (match as little as possible): <.+?> matches <b> then </b> separately.

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex 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