Monday, September 07, 2009

Regular expressions

Useful collection: popular regular expressions

We can add our own. The following JavaScript code recognizes URL's in the text and replaces them with hyperlinks:

var re = new RegExp("(([a-zA-Z]+:\/\/)([a-zA-Z][a-zA-Z0-9_\.-]*[a-zA-Z]{2,6})([a-zA-Z0-9~\#\/\._\?\&%-=]*[a-zA-Z0-9~\#\/_\?\&%-=]))", "g");

text = text.replace(re, "<a href='$1'>$1</a>");

and here we can create mailto hyperlinks:

re = new RegExp("([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4})","g");
text = text.replace(re, "<a href='mailto:$1'>$1</a>");


both tricks borrowed from QR code maker mashup

No comments: