Skip to content

Instantly share code, notes, and snippets.

@d1y
Last active December 21, 2019 07:22
Show Gist options
  • Select an option

  • Save d1y/11c0ad1f152e77c2db2006cec5da3832 to your computer and use it in GitHub Desktop.

Select an option

Save d1y/11c0ad1f152e77c2db2006cec5da3832 to your computer and use it in GitHub Desktop.
判断是否是url
// 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