はじめに
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");