News, tips, tricks and discussions related to web strategy, web design, and custom web application and database development.
Viewing posts for topic: "Javascript". View all posts.
<script type="text/javascript">
//Match email addresses not within tag attributes
var re = /[\w\.\-]+\@([\w\-]+\.)+[\w]{2,4}(?![^<]*>)/ig;
//Function to do replace
function linkEmails(parentNode){
var nodes = parentNode.childNodes;
for (var i=0; i < nodes.length; i++){
//For element nodes other than A nodes, search children
if (nodes[i].nodeType == 1 && nodes[i].tagName != "A") {
linkEmails(nodes[i]);
//For text nodes that match, do replace for parent node.
} else if (nodes[i].nodeType == 3 && re.test(nodes[i].nodeValue)) {
parentNode.innerHTML = parentNode.innerHTML.replace(re,"<a href='mailto:$&'>$&</a>")
}
}
}
//Set onload event to do replace
window.onload = function(){
linkEmails(document.body);
}
</script>
[\w\.\-]+\@([\w\-]+\.)+[\w]{2,4}The next part uses somewhat more advanced regular expression syntax:
(?![^<]*>)The (?!... notation says that the email address will only match is it is not followed by [^<]*>, which should ensure that it is not within a property of an html tag.
Comments (0) Posted on April 29, 2008 10:54:46 AM EDT by Catherine Field