正規表現によるマッチング。location.hrefにmach()使ってドメイン部分を取り出す。 http://www.google.co.jp/ → www.google.co.jp http://example.com/ → example.com file:///D:/→ D http://localhost/hoge/index.html → localhost http://127.0.0.1:8000/ → 127.0.0.1 ※表示の都合上、全角の「?」が混じっているので注意 var str = "http://www.google.co.jp/"; var domain = str.match(/^[httpsfile]+:\/{2,3}([0-9a-z\.\-:]+?):?[0-9]*?\//i)[1]; alert(domain); 変数domainは配列になり、dom
![[javascript] URIからドメインだけ正規表現で取得する](https://cdn-ak-scissors.b.st-hatena.com/image/square/8d71ff5111e05619a10d29bb40d7aebaa75c8fbc/height=288;version=1;width=512/https%3A%2F%2Fs0.wp.com%2Fi%2Fblank.jpg)