Quantcast
Channel: Adam Zwakk » Tutorials
Viewing all articles
Browse latest Browse all 10

Snippet: Parse a plain text email to a clickable link with jQuery

$
0
0

This is pretty simple to do and definitely helps the everyday user enter their email in a regular text box.

I basically check all p tags for an email address using a regular expression and then add an a tag around it with a mailto: link.

Here’s the code:

var emailRegEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/;
$("p").filter(function() {
	return $(this).html().match(emailRegEx);
}).each(function() {
	$(this).html($(this).html().replace(emailRegEx, "<a href=\"mailto:$1\">$1</a>"));
});

Bonus points, parse URLs out of strings:

var siteRegEx = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
$("p").filter(function() {
	return $(this).html().match(siteRegEx);
}).each(function() {
	$(this).html($(this).html().replace(siteRegEx, "<a href=\"$1\">$1</a>"));
});

Viewing all articles
Browse latest Browse all 10

Trending Articles