Master regex patterns for text matching and manipulation.
Regular expressions (regex) are patterns used to match character combinations in strings. They are powerful tools for text processing, validation, and manipulation.
#
a - Matches the character 'a'abc - Matches the string 'abc'a|b - Matches 'a' or 'b'. - Matches any single character except newline^a - Matches 'a' at the start of a stringa$ - Matches 'a' at the end of a string#
[abc] - Matches 'a', 'b', or 'c'[^abc] - Matches anything except 'a', 'b', or 'c'[a-z] - Matches any lowercase letter[A-Z] - Matches any uppercase letter[0-9] - Matches any digit#
a* - Matches zero or more 'a'sa+ - Matches one or more 'a'sa? - Matches zero or one 'a'a{3} - Matches exactly 3 'a'sa{2,4} - Matches 2 to 4 'a's#
``javascript
// Email validation
/^[\w.-]+@[\w.-]+\.\w+$/
// Phone number validation /^\+?[1-9]\d{1,14}$/
// URL validation /https?:\/\/[\w.-]+(?:\.[\w.-]+)+[\w.,@?^=%&:/~+#-]*$/ ``
#
(abc) - Creates a capturing group(?:abc) - Creates a non-capturing group(?abc) - Creates a named capturing group#
(?=abc) - Positive lookahead (matches if followed by 'abc')(?!abc) - Negative lookahead (matches if not followed by 'abc')(?<=abc) - Positive lookbehind (matches if preceded by 'abc')(? - Negative lookbehind (matches if not preceded by 'abc')#
\1 - References the first capturing group\k - References a named capturing group#
g - Global search (find all matches)i - Case insensitivem - Multiline modes - Dotall mode (dot matches newline)u - Unicode mode#
Regex is used for text validation, searching, replacing, and parsing.
The dot matches any single character except newline (unless using the s flag).
* matches zero or more occurrences, + matches one or more occurrences.
Escape it with a backslash: \.
Backtracking is when the regex engine tries different paths to find a match, which can cause performance issues.
Lookahead and lookbehind are zero-width assertions that match a position based on what follows or precedes it.
The i flag makes the regex case-insensitive.
Use capturing groups with parentheses and access them via match indices or named groups.
Try our tools to apply what you have learned in real-time.