Developer

Regex Basics: How Patterns, Flags and Character Classes Work

ToolOrbit Engineering 2 min readUpdated
Regex Basics: How Patterns, Flags and Character Classes Work

Regular expressions are a compact language for describing patterns in text, perfect for validation, search-and-replace, and parsing. They are also famously easy to get subtly wrong. The ToolOrbit Regex Tester lets you write a JavaScript pattern, see matches highlighted live against your sample text, and inspect each capture group as you type.

How a regex is built

A pattern is a mix of literal characters and metacharacters. Character classes match types of characters, quantifiers control repetition, and anchors tie a match to the start or end of a line. Parentheses create capture groups that pull out the parts you care about.

Pattern: (\d{4})-(\d{2})-(\d{2})
Text:    Release date: 2026-06-29
Group 1: 2026   Group 2: 06   Group 3: 29

Flags that change behavior

  • g (global): find all matches, not just the first
  • i (ignore case): match regardless of upper or lower case
  • m (multiline): make the anchors match at line breaks, not just string ends
  • s (dotall): let the dot match newline characters too
  • u (unicode): handle full Unicode code points correctly

The tester shows every match and its groups, so you can immediately see why a pattern caught too much or missed an edge case.

Warning: Watch for catastrophic backtracking. Patterns with nested quantifiers can hang on certain inputs. Prefer specific character classes and avoid overlapping repetition.

Using the Regex Tester

Enter your pattern and toggle the flags you need, then paste sample text to validate against. Live highlighting shows matches in place and the group panel breaks out each capture. Because everything runs in your browser, you can safely test against real log lines or data samples without uploading them anywhere.

Tips for cleaner patterns

Anchor your patterns when you mean to match a whole value, escape characters that have special meaning such as the dot and parentheses, and reach for named groups to make complex expressions readable. Remember that JavaScript regex differs from other engines in places, so test in the engine you will actually deploy to, which is exactly what this tester uses.

The character classes you will live in

Shorthand classes
ClassMatchesWatch out for
\dDigitsMatches Unicode digits too, not just 0–9
\wWord chars [A-Za-z0-9_]Underscore is included
\sWhitespaceIncludes tabs and newlines
\bWord boundaryNot a character — zero-width

Backtracking is where regex silently turns catastrophic: patterns with nested quantifiers like (a+)+ on pathological inputs can take hours (the classic ReDoS). If a pattern works on samples but a large input freezes the tester, that is the smell. Prefix-chedule your alternations, keep quantification flat, and test the worst case you can find before shipping the expression to production.


Tools mentioned

More reading

View all guides
What Is Base64? How Encoding Works and Common Mistakes
Developer

What Is Base64? How Encoding Works and Common Mistakes

What Base64 encoding actually does, where it is used, and the mistakes that cause broken or oversized output.

2 min readUpdated
encodeURI vs encodeURIComponent: Which One to Use
Developer

encodeURI vs encodeURIComponent: Which One to Use

How percent-encoding works, the difference between encoding a whole URL and a single component, and the mistakes that break links.

2 min readUpdated
What Is a UUID? Version 4 vs Version 7 Explained
Developer

What Is a UUID? Version 4 vs Version 7 Explained

What a UUID looks like, why version 4 is the safe default, when version 7 is better, and best practices for IDs.

2 min readUpdated
How SHA-256 and SHA-512 Hashes Work: Hashing vs Encryption
Developer

How SHA-256 and SHA-512 Hashes Work: Hashing vs Encryption

What a hash function does, how to choose between SHA-256 and SHA-512, and why hashing is not encryption.

2 min readUpdated
What Is a JWT? Header, Payload and Signature Explained
Developer

What Is a JWT? Header, Payload and Signature Explained

A JSON Web Token has three parts. Learn what the header, payload and signature contain, how to read the claims, and why decoding is not verifying.

2 min readUpdated
What Is HTML Encoding? Entities & Escaping Explained
Developer

What Is HTML Encoding? Entities & Escaping Explained

Learn how HTML encoding works, why escaping prevents XSS and layout breaks, and how to encode and decode named, decimal, and hex entities.

2 min readUpdated
Unix Time Explained: Seconds, Milliseconds and Time Zones
Developer

Unix Time Explained: Seconds, Milliseconds and Time Zones

What a Unix timestamp is, how to tell seconds from milliseconds, how time zones come into it, and the values worth recognising.

2 min readUpdated
Binary, Octal, Decimal and Hex: How Number Bases Work
Developer

Binary, Octal, Decimal and Hex: How Number Bases Work

How positional number bases work, why very large values need exact arithmetic, and the hex and binary values you meet every day.

2 min readUpdated