Select a Pattern
Pattern Title
Category
Difficulty
/pattern/flags
Pattern description will appear here.
Test Your Pattern
Results
No test performed
0
No matches to display
No text to highlight
JavaScript Methods
Select a method to see how it works with your regex
Regex Tools
Select a tool to get started with advanced regex features.
Complete Regex Cheat Sheet ๐
๐ฉ Flags (Modifiers)
g
Global - Find all matches (not just first)
i
Case Insensitive - Ignore case differences
m
Multiline - ^ and $ match line starts/ends
s
Dotall - . matches newline characters
u
Unicode - Full Unicode support for emojis
y
Sticky - Match from lastIndex position
๐ค Character Classes
\d
Digit (0-9)
\D
Non-digit (anything except 0-9)
\w
Word character (a-z, A-Z, 0-9, _)
\W
Non-word character
\s
Whitespace (space, tab, newline)
\S
Non-whitespace
.
Any character except newline
๐ข Quantifiers
*
Zero or more (0+ occurrences)
+
One or more (1+ occurrences)
?
Zero or one (optional)
{n}
Exactly n times
{n,}
n or more times
{n,m}
Between n and m times
๐ Anchors & Boundaries
^
Start of string/line
$
End of string/line
\b
Word boundary
\B
Non-word boundary
\A
Start of string (absolute)
\Z
End of string (absolute)
๐ฏ Custom Character Classes
[abc]
Any of a, b, or c
[a-z]
Range from a to z
[^abc]
NOT a, b, or c
[a-zA-Z]
Multiple ranges
[a-z0-9]
Combined ranges
[aeiou]
Vowels example
๐ฅ Groups & Capturing
()
Capturing group - captures matched text
(?:)
Non-capturing group - groups without capturing
(?<name>)
Named group - named capture
\1
Backreference - reference to group 1
\k<name>
Named backreference
๐ Lookahead & Lookbehind
(?=)
Positive lookahead - followed by pattern
(?!)
Negative lookahead - NOT followed by pattern
(?<=)
Positive lookbehind - preceded by pattern
(?<!)
Negative lookbehind - NOT preceded by pattern
๐ง Special Characters (Need Escaping)
\.
Literal dot
\*
Literal asterisk
\+
Literal plus
\?
Literal question mark
\\
Literal backslash
\(
Literal parenthesis
\[
Literal bracket
\{
Literal brace
๐ง Email Validation
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
Matches most common email formats
๐ Phone Numbers (US Format)
/^\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})$/
Matches US phone numbers in various formats
๐ URLs
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/
Matches HTTP and HTTPS URLs
๐ IP Address
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
Validates IPv4 addresses
๐ณ Credit Card Numbers
/^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$/
Matches credit card number format
๐ Date (YYYY-MM-DD)
/^\d{4}-\d{2}-\d{2}$/
ISO date format validation
โฐ Time (HH:MM)
/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/
24-hour time format
๐จ Hexadecimal Color
/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
Hex color codes with optional #
๐ Password Strength
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
Strong password requirements
๐ HTML Tags
/<\/?[\w\s]*>|<.+[\W]>/
Matches HTML opening and closing tags
test()
Purpose: Check if pattern exists
Returns: boolean
regex.test(string)
/\d/.test("hello123") // true
match()
Purpose: Find matches
Returns: Array or null
string.match(regex)
"hello123".match(/\d+/) // ["123"]
matchAll()
Purpose: Find all matches with details
Returns: Iterator
string.matchAll(regex)
Array.from("a1b2".matchAll(/\d/g)) // [["1"], ["2"]]
search()
Purpose: Find position of first match
Returns: Index or -1
string.search(regex)
"hello123".search(/\d/) // 5
replace()
Purpose: Replace matches
Returns: New string
string.replace(regex, replacement)
"hello123".replace(/\d+/, "XXX") // "helloXXX"
split()
Purpose: Split string by pattern
Returns: Array
string.split(regex)
"a,b;c".split(/[,;]/) // ["a", "b", "c"]
exec()
Purpose: Execute regex on string
Returns: Array or null
regex.exec(string)
/(\d+)/.exec("hello123") // ["123", "123"]
๐ Extract Data
// Extract all numbers
const numbers = text.match(/\d+/g);
// Extract words starting with capital
const capitalized = text.match(/\b[A-Z]\w*/g);
// Extract email addresses
const emails = text.match(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g);
โ Validation
// Check if string is only digits
const isNumber = /^\d+$/.test(input);
// Check if valid username (alphanumeric + underscore, 3-16 chars)
const isValidUsername = /^[a-zA-Z0-9_]{3,16}$/.test(username);
// Check if contains only letters and spaces
const isName = /^[a-zA-Z\s]+$/.test(name);
๐งน Text Cleaning
// Remove extra whitespace
const cleaned = text.replace(/\s+/g, ' ').trim();
// Remove all non-alphanumeric except spaces
const alphaOnly = text.replace(/[^a-zA-Z0-9\s]/g, '');
// Replace multiple punctuation with single
const singlePunct = text.replace(/[.!?]{2,}/g, '.');
๐ String Manipulation
// Convert camelCase to kebab-case const kebabCase = camelCase.replace(/[A-Z]/g, '-$&').toLowerCase(); // Capitalize first letter of each word const titleCase = text.replace(/\b\w/g, l => l.toUpperCase()); // Insert spaces before capital letters const spaced = text.replace(/([a-z])([A-Z])/g, '$1 $2');
โก Performance Tips
- Compile regex once, reuse many times
- Use non-capturing groups
(?:)when you don't need the capture - Be specific rather than greedy (
.*?instead of.*) - Use character classes instead of alternation when possible
- Anchor patterns to reduce backtracking
๐ ๏ธ Debugging Tools
- regex101.com - Test and debug patterns
- regexr.com - Visual regex builder
- regexpal.com - Simple regex tester