Regular expression patterns and syntax
Character Classes
Match any single character: a, b, or c
/[abc]/ → matches "a" in "abc"
Negated Character Class
Match any character except a, b, or c
/[^abc]/ → matches "d" in "dbc"
Character Range
Match any lowercase letter
/[a-z]/ → matches "a" in "apple"
Digit
Match any digit (0-9)
/\d/ → matches "5" in "abc123"
Non-Digit
Match any non-digit character
/\D/ → matches "a" in "a1b2"
Whitespace
Match whitespace (space, tab, newline)
/\s/ → matches space in "a b"
Non-Whitespace
Match non-whitespace character
/\S/ → matches "a" in "a b"
Word Character
Match word character (a-z, A-Z, 0-9, _)
/\w/ → matches "a" in "a-b"
Zero or More
Match 0 or more of previous element
/ab*c/ → matches "ac", "abc", "abbc"
One or More
Match 1 or more of previous element
/ab+c/ → matches "abc", "abbc" (not "ac")
Zero or One
Match 0 or 1 of previous element
/ab?c/ → matches "ac", "abc"
Exactly N Times
Match exactly n times
/a{3}/ → matches "aaa"
N to M Times
Match between n and m times
/a{2,4}/ → matches "aa", "aaa", "aaaa"
Alternation
Match either left or right
/cat|dog/ → matches "cat" or "dog"
Grouping
Group subpattern
/(ab)+/ → matches "ab", "abab"
Start of String
Match start of string
/^hello/ → matches "hello world"
End of String
Match end of string
/world$/ → matches "hello world"
Any Character
Match any character (except newline)
/a.c/ → matches "abc", "adc"
Email Pattern
Simple email validation
/[^@]+@[^@]+\.[^@]+/ → matches "user@example.com"
URL Pattern
Match HTTP/HTTPS URLs
/https?://[\w.-]+/ → matches "https://example.com"