📝 Tutorials

What is Regex? A Simple Explanation for Developers


Regex (regular expressions) is a pattern language for matching text. It lets you search, validate, and extract data from strings using patterns instead of exact matches.

// Does this string contain an email?
/[\w.-]+@[\w.-]+\.\w+/.test("contact me at alice@example.com")  // true

// Extract all numbers from a string
"Order #123, total $45.99".match(/\d+\.?\d*/g)  // ["123", "45.99"]

Common patterns you’ll actually use

\d        Any digit (0-9)
\w        Any word character (letters, digits, underscore)
\s        Any whitespace (space, tab, newline)
.         Any character except newline
^         Start of string
$         End of string
+         One or more
*         Zero or more
?         Zero or one (optional)
{3}       Exactly 3 times
{2,5}     Between 2 and 5 times
[abc]     Any of a, b, or c
[^abc]    Not a, b, or c
(group)   Capture group
a|b       a or b

Real-world examples

// Email (basic)
/^[\w.-]+@[\w.-]+\.\w{2,}$/

// URL
/https?:\/\/[\w.-]+\.\w{2,}(\/\S*)?/

// Phone number (US)
/\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/

// Date (YYYY-MM-DD)
/\d{4}-\d{2}-\d{2}/

// IP address (basic)
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/

// Hex color
/#[0-9a-fA-F]{3,6}/

When to use regex

Good for: validation (emails, phone numbers), search and replace, extracting data from text, log parsing, quick text transformations.

Bad for: parsing HTML (use a DOM parser), complex nested structures (use a real parser), anything where readability matters more than cleverness.

The readability problem

Regex is write-only code. This is valid regex:

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$

Nobody can read that. When regex gets complex, use named groups, comments, or just write normal code instead.

Try patterns interactively with our Regex Tester tool. For the full syntax reference, see the Regex cheat sheet.