~/devtools / regex / url
tool::regex-guide

regex url

/^https?:\/\/[^\s\/$.?#].[^\s]*$/i

URL検証

ad · 728×90
正規表現パターン
/^https?:\/\/[^\s\/$.?#].[^\s]*$/i
フラグ:i
パターン解析
^https?http or https scheme
:\/\/Literal ://
[^\s\/$.?#]First domain char: not whitespace, slash, dot, $, ?, #
.Any single character
[^\s]*Rest of URL: any non-whitespace characters
$End of string
マッチ例
✓https://example.com
✓http://sub.domain.org/path?q=1
✓https://api.site.io/v1/users
非マッチ例
✗ftp://file.server.com
✗example.com
✗https://
✗//no-scheme.com

httpまたはhttpsプロトコルで始まるURLを検証するパターンです。ドメイン、パス、クエリ文字列などの標準的なURL構成要素を許可します。ftpやmailtoなどの他のスキームには別途パターンが必要です。

言語別の使用方法

JavaScript— RegExpオブジェクトまたはリテラル構文を使用
const pattern = /^https?:\/\/[^\s\/$.?#].[^\s]*$/i;

// Test a string
console.log(pattern.test("https://example.com")); // true
console.log(pattern.test("ftp://file.server.com")); // false

// Match and extract
const result = "https://example.com".match(pattern);
Python— reモジュールを使用
import re

pattern = r"^https?:\/\/[^\s\/$.?#].[^\s]*$"
flags = re.IGNORECASE

# Test a string
if re.fullmatch(pattern, "https://example.com", flags):
    print("Valid")

# Find all matches in text
matches = re.findall(pattern, text, flags)
Java— java.util.regexパッケージを使用
import java.util.regex.*;

String pattern = "^https?:\\/\\/[^\\s\\/$.?#].[^\\s]*$";
Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);

Matcher m = p.matcher("https://example.com");
boolean isValid = m.matches(); // true

// Find occurrences
Matcher finder = p.matcher(inputText);
while (finder.find()) {
    System.out.println(finder.group());
}

主なユースケース

  • ▸ウェブサイトURL入力フィールドの検証
  • ▸リンクリストから有効なURLをフィルタリング
  • ▸クローラーのURL形式事前検証
  • ▸ユーザープロフィールのホームページURL検証
  • ▸Webhook URL登録前の有効性確認

関連パターン

email/^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-…/i→phone/^\+?[\d\s\-\(\)]{7,15}$/→ip-address/^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.…/→date/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[…/→
ad · 300×250
← Regexテスターに戻る
// related tools
⏱
Cron Expression Generator
ビジュアルエディタでcron式を作成・解析。次回5回の実行時刻をプレビューし、よく使うプリセットから始められます。
jwt
JWT Decoder
JWTトークンをデコードして内容を確認。ヘッダー・ペイロード・署名の詳細を表示します。
◑
Color Converter
HEX・RGB・HSL形式でカラーコードを変換。カラーピッカーで視覚的に選択できます。
ts
Timestamp Converter
Unixタイムスタンプを人間が読める日付に変換。ms/s・UTC/ローカル・相対時刻に対応。