How to extract the host from a URL in JavaScript?

后端 未结 5 2118
我寻月下人不归
我寻月下人不归 2021-02-12 18:20

Capture the domain till the ending characters $, \\?, /, :. I need a regex that captures domian.com in all of these.

domain.com:3000
do         


        
5条回答
  •  渐次进展
    2021-02-12 18:44

    A new challenger has appeared. According to node docs, you can also use

       var url = new URL(urlString);
       console.log(url.hostname);
    

    https://nodejs.org/api/url.html#url_the_whatwg_url_api

    This seems to be a more current way.

提交回复
热议问题