Last active
October 2, 2017 15:14
-
-
Save sohkai/5c8f862b4496b41dc681 to your computer and use it in GitHub Desktop.
Javascript URL Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\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