LoginSignup
7
8

More than 5 years have passed since last update.

サブドメイン有り無しドメインにマッチするパターン

Last updated at Posted at 2013-07-09

はじめに

hoge.com, www.hoge.com, api.hoge.com, stg.api.hoge.com全てにマッチするパターンを書いてみた。

作成したパターン

// パターン
var regexp = new RegExp("^(.+[^.]\.)?hoge\.com$");

// 正常系
console.log("hoge.com".match(regexp) ? "OK" : "NG");
console.log("www.hoge.com".match(regexp) ? "OK" : "NG");
console.log("api.hoge.com".match(regexp) ? "OK" : "NG");
console.log("stg.api.hoge.com".match(regexp) ? "OK" : "NG");

// 異常系
console.log(!" hoge.com".match(regexp) ? "OK" : "NG");
console.log(!".hoge.com".match(regexp) ? "OK" : "NG");
console.log(!"hoge1.com".match(regexp) ? "OK" : "NG");
console.log(!"hoge.jp".match(regexp) ? "OK" : "NG");
console.log(!"hoge.com/".match(regexp) ? "OK" : "NG");
console.log(!"hogecom".match(regexp) ? "OK" : "NG");
console.log(!"..hoge.com".match(regexp) ? "OK" : "NG");
7
8
6

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
7
8