\
) to match the character itself.// Example regex to match an email address
[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}
This regex will match an email address in the format username@domain.com
.
// Example regex to match a phone number
\+?\d{1,3}[-. ]?\d{3}[-. ]?\d{4}
This regex will match a phone number in a variety of formats, including 123-456-7890
, 123.456.7890
, and +1 123-456-7890
.
// Example regex to match a URL
https?://[\w\-\.]+(:\d+)?(/[\w/_\.]*)?
This regex will match a URL in a variety of formats, including http://www.example.com
, https://example.com/path/to/page.html
, and http://www.example.com:8080/path/to/page.html
.
.
: Matches any single character except a newline.^
: Matches the start of a string.$
: Matches the end of a string.*
: Matches zero or more occurrences of the previous character.+
: Matches one or more occurrences of the previous character.?
: Matches zero or one occurrence of the previous character.|
: Matches either the character on the left or the character on the right.[]
: Matches any one character within the brackets.()
: Groups characters together.\d
: Matches any digit character.\w
: Matches any word character (alphanumeric and underscore).\s
: Matches any whitespace character.\D
: Matches any non-digit character.\W
: Matches any non-word character.\S
: Matches any non-whitespace character.{n}
: Matches exactly n occurrences of the previous character.{n,}
: Matches n or more occurrences of the previous character.{n,m}
: Matches between n and m occurrences of the previous character.(?=...)
) and lookbehinds ((?<=...)
) can be used to match characters based on what precedes or follows them.\1
, \2
, etc.) can be used to refer to previously matched groups.