Skip to content

Instantly share code, notes, and snippets.

@juanbrujo
Last active December 24, 2015 08:59
Show Gist options
  • Save juanbrujo/6773848 to your computer and use it in GitHub Desktop.
Save juanbrujo/6773848 to your computer and use it in GitHub Desktop.
Find emails inside a string and transforms them in clickable
$(".real-content")
.find (':contains("@")')
.each (function (index, element) {
var emailPattern = /[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/;
var corrected = $(element).html();
var remainder = corrected;
while ( emailPattern.test (remainder)) {
var email = emailPattern.exec (remainder);
remainder = remainder.replace (email, '');
corrected = corrected.replace (email, '<a href="mailto:' + email + '">' + email + '</a>');
}
$(element).html(corrected);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment