Skip to content

Instantly share code, notes, and snippets.

@pandringa
Last active August 29, 2015 14:05
Show Gist options
  • Save pandringa/5960a1cfc8f2dba0bf1b to your computer and use it in GitHub Desktop.
Save pandringa/5960a1cfc8f2dba0bf1b to your computer and use it in GitHub Desktop.
Useful Regexes

Search

/^(?=.*\b<word1>)(?=.*\<word2>)/ - Good for typeahead search with MongoDB, because it matches words in any order. To use, split a query string by spaces, then insert each word into the regex surrounded by parenthesies with forward lookahead ((?=.* )). Here, I've included the \b which matches the start of a word, to ensure users' results aren't cluttered by random letters in the middle of words. (http://rubular.com/r/hnOalIdRYA)

URLs

https://gist.github.com/gruber/8891611 - Best one I've seen for matching web URLS

Emails

\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b - Should match just about any email address, except for anyone that decides to put an MX record on a TLD (for example, admin@com would be a possible address that doesn't match this). However, for 99.999% of all use cases, this is preferrable (to make sure that people don't forget to add .com to the end).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment