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
| Token | Description |
|---|---|
| m | Matches the literal character 'm' |
| a | Matches the literal character 'a' |
| i | Matches the literal character 'i' |
| l | Matches the literal character 'l' |
| t | Matches the literal character 't' |
| o | Matches 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
| Input | Expected |
|---|---|
| mailto:user@example.com | Match |
| mailto:info@site.org?subject=Hello | Match |
| user@example.com | No Match |
| mailto:invalid@ | No Match |
| mailto:a.b+c@domain.co.uk | Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
71 charsFlags: giMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex for Email Validation
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/i
Regex to Match URLs
/https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:\/[\w./?#&=%-]*)*/gi
Regex to Match HTML Tags
/<\/?([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?>/g
Regex to Extract Domain Names
/(?:https?:\/\/)?(?:www\.)?([a-zA-Z0-9-]+(?:\.[a-zA-Z]{2,})+)/gi