Skip to content

Instantly share code, notes, and snippets.

@sohkai
Last active October 2, 2017 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sohkai/5c8f862b4496b41dc681 to your computer and use it in GitHub Desktop.
Save sohkai/5c8f862b4496b41dc681 to your computer and use it in GitHub Desktop.
Javascript URL Regex
\b(https?:\/\/)?((?:www\.)?((?:[^\s.,;()\/]+\.)+[^\s$_!*()$&.,;=?+\/\#]+)((?:\/|\?|\/\?)[^\s#^`{}<>?"\[\]\/\|]+)*\/?(#[^\s#%^`{}<>?"\[\]\/\|]*)?)
/**
* Should be used with /i flag as well as the /g flag if looking for multiple matches
*
* Notes:
* Ignores schemes other than http(s).
* Captures emails as otherwise it would match the `gmail.com` in `hi@gmail.com`,
* producing (what I think is) more surprising behaviour than the alternative.
*
* Produces some false positives on tests from https://mathiasbynens.be/demo/url-regex,
* but most of these false positives are meant to pass for practical reasons
* (such as foo.com, http://-error-.com, etc). However this does mean that any
* string of the format \(\w*.)+\w will pass this filter (most noticable for property
* accessors or mistyped sentences... you can't win them all ¯\_(ツ)_/¯).
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment