Skip to content

Instantly share code, notes, and snippets.

@andyedinborough
Created January 22, 2012 16:49
Show Gist options
  • Save andyedinborough/1657642 to your computer and use it in GitHub Desktop.
Save andyedinborough/1657642 to your computer and use it in GitHub Desktop.
One RegExp to match urls, email addresses, Twitter usernames and hashtags.
function combine() {
var ret = '', flags = '';
[ ].forEach.call(arguments, function (exp) {
exp = exp.toString();
if (exp[0] !== '/') {
flags = exp;
} else {
ret += exp.substr(1, exp.length - 2);
}
});
return new RegExp(ret, flags);
}
var rxALL = combine(/(?=\w)/,
/*Email *//([a-z0-9_\-\.]+@(?:[a-z0-9\-]+\.)+[a-z\-]+)|/,
/*URL *//((?:(?:https?\:\/\/)?(?:[a-z0-9\-]+\.)+[a-z]{2,3}(?!\w))(?:[\S]*[a-z0-9\/=&])?)|/,
/*Username *//(@[a-z0-9_]+)|/,
/*Tag *//(#[a-z0-9_]{2,})/,
/(?!\w)/, 'gi');
var rxALL = /(?=\w)([a-z0-9_\-\.]+@(?:[a-z0-9\-]+\.)+[a-z\-]+)|((?:(?:https?\:\/\/)?(?:[a-z0-9\-]+\.)+[a-z]{2,3}(?!\w))(?:[\S]*[a-z0-9\/=&])?)|(@[a-z0-9_]+)|(#[a-z0-9_]{2,})(?!\w)/gi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment