Last active
December 21, 2019 07:22
-
-
Save d1y/11c0ad1f152e77c2db2006cec5da3832 to your computer and use it in GitHub Desktop.
判断是否是url
This file contains hidden or 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
| // source: https://github.com/segmentio/is-url/blob/master/index.js | |
| export const isUrl = (str: string): boolean => { | |
| let protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/ | |
| let localhostDomainRE = /^localhost[\:?\d]*(?:[^\:?\d]\S*)?$/ | |
| let nonLocalhostDomainRE = /^[^\s\.]+\.\S{2,}$/ | |
| if (typeof str !== 'string') return false | |
| let match = str.match(protocolAndDomainRE) | |
| if (!match) return false | |
| let everythingAfterProtocol = match[1] | |
| if (!everythingAfterProtocol) return false | |
| if (localhostDomainRE.test(everythingAfterProtocol) || nonLocalhostDomainRE.test(everythingAfterProtocol)) { | |
| return true | |
| } | |
| return false | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment