Regex to Match Mailto Links

Match mailto links in HTML or text with this regex pattern. Captures email addresses with optional subject and body parameters. Free tool.

Regular Expression

/mailto:[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(?:\?[\w=&%.+-]*)?/gi

Token Breakdown

TokenDescription
mMatches the literal character 'm'
aMatches the literal character 'a'
iMatches the literal character 'i'
lMatches the literal character 'l'
tMatches the literal character 't'
oMatches the literal character 'o'
:Matches the literal character ':'
[a-zA-Z0-9._%+-]Character class — matches any one of: a-zA-Z0-9._%+-
+Matches the preceding element one or more times (greedy)
@Matches the literal character '@'
[a-zA-Z0-9.-]Character class — matches any one of: a-zA-Z0-9.-
+Matches the preceding element one or more times (greedy)
\.Matches a literal dot
[a-zA-Z]Character class — matches any one of: a-zA-Z
{2,}Matches 2 or more times
(?:Start of non-capturing group
\?Matches a literal question mark
[\w=&%.+-]Character class — matches any one of: \w=&%.+-
*Matches the preceding element zero or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)

Detailed Explanation

This regex matches mailto links commonly found in HTML href attributes or plain text. Here is the token-by-token breakdown:

mailto: — Matches the literal string 'mailto:' which is the URI scheme for email links. This prefix distinguishes mailto links from regular email addresses.

[a-zA-Z0-9._%+-]+ — Matches the local part of the email address (before the @). It allows letters, digits, dots, underscores, percent signs, plus signs, and hyphens. The + quantifier requires at least one character.

@ — Matches the literal at-sign separating the local part from the domain.

[a-zA-Z0-9.-]+ — Matches the domain name, allowing letters, digits, dots for subdomains, and hyphens.

. — Matches a literal dot before the top-level domain.

[a-zA-Z]{2,} — Matches the top-level domain, requiring at least two letters.

(?:?[\w=&%.+-]*)? — An optional non-capturing group that matches query parameters. Mailto links can include parameters like ?subject=Hello&body=World. The group starts with a literal question mark, followed by zero or more allowed characters including word characters, equals signs, ampersands, percent signs, dots, plus signs, and hyphens.

The g flag enables global matching for finding all mailto links in the text, and the i flag makes it case-insensitive. This pattern is useful for scraping or validating mailto links in HTML documents.

Example Test Strings

InputExpected
mailto:user@example.comMatch
mailto:info@site.org?subject=HelloMatch
user@example.comNo Match
mailto:invalid@No Match
mailto:a.b+c@domain.co.ukMatch

Try It — Interactive Tester

//gi
gimsuy

Match Highlighting(3 matches)

mailto:user@example.com mailto:info@site.org?subject=Hello user@example.com mailto:invalid@ mailto:a.b+c@domain.co.uk

Matches & Capture Groups

#1mailto:user@example.comindex 0
#2mailto:info@site.org?subject=Helloindex 24
#3mailto:a.b+c@domain.co.ukindex 92
Pattern: 71 charsFlags: giMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →