Regular expressions (RegExp) are patterns used for string matching.
Metacharacters (Special Characters)
.
→ Matches any character except newline.^
→ Matches the start of a string.$
→ Matches the end of a string.\\d
→ Matches any digit ([0-9]
).\\w
→ Matches any word character ([a-zA-Z0-9_]
).\\s
→ Matches whitespace.\\b
→ Matches a word boundary.\\
→ Escapes a special character (e.g., \\.
matches a dot .
).Quantifiers
/a*/
matches "a"
, "aa"
, ""
).+
→ 1 or more times (/a+/
matches "a"
, "aa"
, but not ""
).?
→ 0 or 1 time (/a?/
matches "a"
or ""
).{n}
→ Exactly n
times (/\\d{3}/
matches "123"
).{n,}
→ At least n
times (/\\d{2,}/
matches "12"
, "123"
, "1234"
).{n,m}
→ Between n
and m
times (/\\d{2,4}/
matches "12"
, "123"
, "1234"
).Groups & Character Classes
(abc)
→ Capturing group (/(ab)+/
matches "abab"
).