MozTW 討論區
https://forum.moztw.org/

用 RegExp 抓 URL 中的網域名稱?
https://forum.moztw.org/viewtopic.php?f=18&t=12510
1 頁 (共 1 頁)

發表人:  yuoo2k [ 2006-01-21, 03:00 ]
文章主題 :  用 RegExp 抓 URL 中的網域名稱?

目的是利用 RegExp 抓出 URL 中的網域 (Domain) 名稱~

比如說 http://forum.moztw.org:80/posting.php
需要抓出 forum.moztw.org

我現在的寫法:
代碼:
getUrlDomain: function(url) {
   var matches = url.match(/^([A-Za-z]+:\/+)*([^\:^\/]+):?(\d*)(\/.*)*/);
   return (matches ? matches[2] : url);
}

不知上述寫法有無問題? or 是否有更好的 RegExp 表示式能做這件事?

參考:
http://developer.mozilla.org/en/docs/Co ... cts:RegExp

發表人:  kourge [ 2006-01-21, 03:30 ]
文章主題 : 

The only thing I can think of is to replace [A-Za-z] with \w

1 頁 (共 1 頁) 所有顯示的時間為 UTC + 8 小時
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/