文字列がURLの形式であるか、またはURLからサービスを判定したい時の正規表現まとめ

入力のバリデーションとかでよく使うのでメモしておきます。

/**
 * URLであるか判定する正規表現
 */
const regexUrl = /^https?:\/\/[\w/:%#$&?()~.=+\-]+$/i

/**
 * GitHubのURLであるかを判定する正規表現
 */
const regexUrlGithub = /^https?:\/\/(www\.)?github\.com/i

/**
 * FacebookのURLであるかを判定する正規表現
 */
const regexUrlFacebook = /^https?:\/\/(www\.)?(facebook|fb)\.(com|me)/i

/**
 * TwitterのURLであるかを判定する正規表現
 */
const regexUrlTwitter = /^https?:\/\/(mobile\.|www\.)?twitter\.com/i

/**
 * YoutubeのURLであるかを判定する正規表現
 */
const regexUrlYoutube = /^https?:\/\/(www\.)?youtube\.com/i

/**
 * InstagramのURLであるかを判定する正規表現
 */
const regexUrlInstagram = /^https?:\/\/(www\.)?instagram\.com/i

今後、変更・追記していくかも。