Why Learn Regular Expressions?
Regular expressions (regex) are a mini-language for describing text patterns. Once you understand them, tasks that would take dozens of lines of code β like validating an email address, extracting phone numbers from a document, or replacing all dates in a specific format β become single-line operations.
Almost every programming language supports regex: JavaScript, Python, Java, PHP, Go, Ruby, and command-line tools like grep and sed.
Core Syntax in 5 Minutes
Literal characters: Most characters match themselves.
hello matches the string "hello" anywhere in the text.
Dot (.) β matches any single character (except newline):
h.llo matches "hello", "hallo", "h3llo"
Character classes ([...]) β matches any one character inside:
[aeiou] matches any vowel
[0-9] matches any digit
[a-zA-Z] matches any letter
Shorthand classes:
\dβ any digit (same as[0-9])\wβ word character (letters, digits, underscore)\sβ whitespace (space, tab, newline)\D,\W,\Sβ the negated versions
Quantifiers:
+β one or more*β zero or more?β zero or one (optional){3}β exactly 3 times{2,5}β between 2 and 5 times
Anchors:
^β start of string$β end of string
5 Practical Regex Patterns Every Developer Needs
Email validation:
`
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
`
US phone number:
`
^\+?1?[-.\s]?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}$
`
URL:
`
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}
`
IPv4 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]?)$
`
Date (YYYY-MM-DD):
`
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
`
Test Regex in Real-Time
The best way to learn regex is to experiment. FuaHub's Regex Tester gives you:
1. A pattern input where you write your regex.
2. A test string area where matches are highlighted in real-time.
3. A capture groups panel that shows what each group captured.
Open the [Regex Tester](/tools/dev/regex-tester) and try pasting any of the patterns above to see them in action.
Common Beginner Mistakes
- Forgetting to escape special characters: In regex,
.means "any character." To match a literal dot, write\. - Being too greedy:
.*matches as much as possible. Use.*?for non-greedy matching. - Not anchoring patterns:
\d+matches any sequence of digits anywhere in text. Add^and$if you need to match the entire string.