0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【JavaScript】URL.canParse()の使い方

Posted at

URL.canParse()を使用する

URL.canParse()の引数にURL文字列を渡すことで有効か判定できます。

function isValidURL(url) {
  return URL.canParse(url)
}

URLの有効性を判定する他の方法

URL()コンストラクターが与えられたURL文字列が有効でなければ例外をスローすることを利用して以下のように判定することもできます。

function isValidURL(url) {
  try {
    new URL(url)
    return true
  } catch {
    return false
  }
}

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?