This regex cheat sheet covers the patterns developers and data analysts reach for most often - email, URL, phone, date, IP, hex color, and more - with a syntax reference you can test immediately in TextSorter’s free online Regex Tester.
What Do Regex Syntax Symbols Mean?
Character Classes
.- Any single character except newline\d- Any digit (0-9). Equivalent to[0-9]\D- Any non-digit character\w- Any word character (letters, digits, underscore)\W- Any non-word character\s- Any whitespace (space, tab, newline)[abc]- Any one of a, b, or c[^abc]- Any character NOT a, b, or c[a-z]- Any lowercase letter a through z
Quantifiers
*- 0 or more times (greedy)+- 1 or more times (greedy)?- 0 or 1 time (optional){"{n}"}- Exactly n times{"{n,m}"}- Between n and m times*?/+?- Lazy (non-greedy) - stops at shortest match
Anchors and Boundaries
^- Start of line$- End of line\b- Word boundary
Groups
(abc)- Capturing group(?:abc)- Non-capturing group (faster, no backreference)a|b- Match “a” OR “b”(?=abc)- Positive lookahead(?!abc)- Negative lookahead
What Are the Most Useful Regex Patterns?
Email Address
“
Matches: user@example.com, first.last+tag@subdomain.co.uk Note: No regex can fully validate email - only sending a confirmation message can.
URL (HTTP/HTTPS)
“
Matches: https://example.com/path?query=1, http://sub.domain.org
US Phone Number
“
Matches: (555) 123-4567, 555.123.4567, +1 555 123 4567, 5551234567
International Phone (E.164)
“
Matches: +14155552671, +447911123456, +33123456789
IPv4 Address
“
Matches: 192.168.1.1, 255.255.255.0 · Rejects: 999.1.1.1
Date - YYYY-MM-DD (ISO 8601)
“
Matches: 2026-03-15 · Rejects: 2026-13-01 (month 13)
Date - MM/DD/YYYY (US)
“
Matches: 03/15/2026, 12/31/1999
Time - HH:MM (24-hour)
“
Matches: 09:30, 23:59 · Rejects: 25:00
Hex Color Code
“
Matches: #1a2b3c, #fff, #FF6347
US ZIP Code
“
Matches: 90210, 10001-1234
Credit Card (16-digit format)
“
Note: Matches format only. Use a Luhn algorithm to validate the actual number.
HTML Tag
“
Matches: <p>, <div class="wrap">, </span>
Blank Lines
“
Use case: Find & remove empty lines using your editor’s regex-enabled Find & Replace.
Repeated Words
“
Matches: the the, is is - catches accidental double words in writing.
What Are Regex Flags and When Should You Use Them?
i- Case-insensitive./hello/imatches “Hello”, “HELLO”, “hello”.g- Global. Find all matches, not just the first one.m- Multiline. Makes^and$match the start/end of each line.s- Dotall. Makes.match newline characters too.
Quick Tips for Writing Reliable Regex
- Always escape special characters when matching them literally - write
\.to match a period, not.which matches any character. - Use non-capturing groups
(?:...)when you need to group without capturing - it’s faster and cleaner. - Prefer lazy quantifiers
+?when extracting content between delimiters to avoid over-matching. - Test on real data - paste your actual text into TextSorter’s Regex Tester before applying patterns in production.
Frequently Asked Questions
What’s the difference between a capturing group and a non-capturing group?
A capturing group (abc) stores the matched text for use in backreferences or replace operations. A non-capturing group (?:abc) groups the pattern without storing the result - it’s more efficient when you only need grouping for quantifier purposes.
Why does my regex match too much text?
You’re likely using a greedy quantifier (+ or *). Switch to a lazy version (+? or *?) to stop at the shortest match. This is especially common when extracting content between HTML tags.
Test These Patterns in the Free Regex Tester →
In-Depth Architectural Guide: How Modern Text Processing Engines Work Under the Hood
When manipulating text, formatting strings, or extracting tokens in web applications, understanding how the underlying runtime engine processes character streams is essential for building scalable software.
In modern JavaScript engines (such as Google V8, Apple JavaScriptCore, and Mozilla SpiderMonkey), strings are stored in optimized memory structures:
+---------------------+-------------------+-------------------------------+-------------------------+
| String Representation| Memory Structure | Performance Advantage | Typical Use Case |
+---------------------+-------------------+-------------------------------+-------------------------+
| Flat ASCII String | 1 Byte / Char | Ultra-low memory cache density| Standard English text |
| Two-Byte UTF-16 | 2 Bytes / Char | Universal Unicode code points | International & Emojis |
| ConsString | Tree of 2 strings | O(1) Instant Concatenation | Repeated string joins |
| SlicedString | Pointer + Offset | O(1) Zero-Copy Substrings | Parsing large payloads |
+---------------------+-------------------+-------------------------------+-------------------------+
1. The ConsString Concatenation Optimization
When you join strings repeatedly in a loop (str += chunk), V8 does not immediately copy all bytes into a new flat array. Instead, it creates a ConsString (a lightweight binary tree node referencing the two parent strings). Only when you perform a search, regex match, or export does the engine flatten the tree into contiguous memory.
2. SlicedString: High-Speed Substring Extraction
When extracting tokens or substrings from a 10-megabyte text document using str.slice(start, end), V8 creates a SlicedString containing a memory pointer to the original parent string and the start/end integer offsets. This enables instant substring extraction with zero memory allocation.
Common Pitfalls and Edge Cases in Text Manipulation
- Surrogate Pair Truncation: When slicing strings containing multi-byte characters or emojis (
🚀,👩💻), naive character slicing can sever surrogate pairs, creating corrupted replacement characters (“). Always use Unicode-aware iteration (Array.from(str)or[...str]). - Regex Catastrophic Backtracking: Writing poorly bounded regular expressions with nested quantifiers (like
(a+)+$) on untrusted user input can cause exponential CPU backtracking, locking up server worker threads. Always set strict input size limits or use atomic lookahead assertions. - Memory Leaks in Closures: Retaining small SlicedString tokens extracted from gigantic parent strings inside long-lived closures prevents the entire multi-megabyte parent string from being garbage collected. Always flatten or copy retained tokens.
Step-by-Step Practical Tutorial: Building High-Performance Client-Side Utilities
Here is a production-ready JavaScript class demonstrating efficient text transformations with zero external npm dependencies:
class HighPerformanceTextProcessor {
constructor(rawText = '') {
this.text = rawText;
}
cleanWhitespace() {
this.text = this.text
.replace(/[\r\n]+/g, '\n')
.replace(/[^\S\r\n]+/g, ' ')
.trim();
return this;
}
deduplicateLines(caseSensitive = false) {
const lines = this.text.split('\n');
const seen = new Set();
const unique = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const key = caseSensitive ? line : line.toLowerCase();
if (!seen.has(key)) {
seen.add(key);
unique.push(line);
}
}
this.text = unique.join('\n');
return this;
}
getWordCount() {
if (!this.text.trim()) return 0;
return this.text.trim().split(/\s+/).length;
}
toString() {
return this.text;
}
}
Interactive Frequently Asked Questions (FAQ)
1. Why are 100% client-side text tools safer for sensitive corporate data?
Because traditional online text tools send your pasted text over public HTTP connections to remote cloud servers where it can be logged in databases, cached on proxies, or exposed in server logs. TextSorter executes all transformations entirely inside your local browser memory (RAM), guaranteeing that sensitive customer data, API keys, and private documents never leave your physical device.
2. Can I use these text utilities when working offline without internet access?
Yes! TextSorter is an installable Progressive Web App (PWA). Once loaded, the Service Worker caches all scripts and Web Workers locally on your machine, allowing you to clean, sort, format, and convert text on airplanes, trains, or secure offline environments.
3. How do Web Workers prevent browser UI tabs from freezing during large operations?
JavaScript is single-threaded on the main DOM thread. When processing large datasets with hundreds of thousands of rows, executing number-crunching loops on the main thread blocks UI rendering. Web Workers execute tasks in isolated background threads, keeping the browser UI completely smooth and responsive at 60 frames per second.
Summary Checklist for Clean Production Text Operations
- Verify UTF-8 Encoding: Ensure your application specifies UTF-8 encoding across HTML, database collations, and HTTP response headers.
- Handle Special Characters: Use standard entity encodings or parameterized queries to prevent injection vulnerabilities.
- Audit Performance: Use Web Workers for datasets exceeding 50,000 rows to maintain silky-smooth UI responsiveness.
- Use Privacy-First Tools: Process confidential files using TextSorter Tools. Everything runs 100% locally in your browser memory for total confidentiality.