What is Regex — Regular Expressions Explained
Regular expressions look intimidating but they are one of the most powerful tools a developer can learn. Here is everything you need to know.
Test regex patterns instantly
Use our free Regex Tester — live match highlighting as you type.
What is a regular expression?
A regular expression (regex) is a sequence of characters that defines a search pattern. It is used to match, find, replace, or validate strings in text.
Basic regex syntax
.Match any single character except newline
*Match 0 or more of the preceding character
+Match 1 or more of the preceding character
?Match 0 or 1 of the preceding character
^Match start of string
$Match end of string
\dMatch any digit (0-9)
\wMatch any word character (a-z, A-Z, 0-9, _)
\sMatch any whitespace character
[abc]Match any character in the set
[^abc]Match any character NOT in the set
Common regex patterns
Email validation
^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$Phone number (US)
^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$URL
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}Digits only
^\d+$Alphanumeric
^[a-zA-Z0-9]+$Strong password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$Regex in JavaScript
// Test if string matches pattern
/^\d+$/.test("12345") // true
/^\d+$/.test("abc") // false
// Find matches
"hello world".match(/\w+/g)
// ["hello", "world"]
// Replace
"hello world".replace(/world/, "regex")
// "hello regex"