Javascript 如何拆分任何url并仅获取源?

Javascript 如何拆分任何url并仅获取源?,javascript,regex,split,Javascript,Regex,Split,我想拆分https://abcnews.go.com/此类型的URL 想获得abcnews 我用过: .split('//')[1] .split('www.')[1] .split('.com')[0] .split('.org')[0] .split('.edu')[0] .split('.info')[0] 我得到的错误不能分割 你知道怎么做吗?你接受Regex的回答吗 consturl='1〕https://abcnews.go.com/'; 让子域=url.match(/(\w*)

我想拆分
https://abcnews.go.com/
此类型的URL

想获得abcnews

我用过:

.split('//')[1]
.split('www.')[1]
.split('.com')[0]
.split('.org')[0]
.split('.edu')[0]
.split('.info')[0]
我得到的错误不能分割


你知道怎么做吗?

你接受Regex的回答吗

consturl='1〕https://abcnews.go.com/';
让子域=url.match(/(\w*)\./)[1];
console.log(子域)

//subdomain=“abcnews”
您接受Regex的回复吗

consturl='1〕https://abcnews.go.com/';
让子域=url.match(/(\w*)\./)[1];
console.log(子域)
//subdomain=“abcnews”
这看起来很安全:

(?:https?:\/\/(?:www\.)?|www\.)([^.\/]+)

解释

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    http                     'http'
--------------------------------------------------------------------------------
    s?                       's' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      www                      'www'
--------------------------------------------------------------------------------
      \.                       '.'
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    www                      'www'
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^.\/]+                  any character except: '.', '\/' (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
JavaScript代码

consturl=”https://abcnews.go.com/"
常量正则表达式=/(?:https?:\/\/(?:www\)?;www\)([^.\/]+)/
const match=url.match(正则表达式)
如果(匹配){
console.log(匹配[1])
}
这看起来很安全:

(?:https?:\/\/(?:www\.)?|www\.)([^.\/]+)

解释

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    http                     'http'
--------------------------------------------------------------------------------
    s?                       's' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      www                      'www'
--------------------------------------------------------------------------------
      \.                       '.'
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    www                      'www'
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^.\/]+                  any character except: '.', '\/' (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
JavaScript代码

consturl=”https://abcnews.go.com/"
常量正则表达式=/(?:https?:\/\/(?:www\)?;www\)([^.\/]+)/
const match=url.match(正则表达式)
如果(匹配){
console.log(匹配[1])

}
s.match(/:\/\/([^.\/]+)/)[1]
如果url有www.something.com,则我只收到www请根据您的所有要求更新问题。请尝试
(?:https?:\/\/(?:www\)?(www\)([^.\/]+)/
,请参阅。
s.match(/:\/\/\/([^.\/]+)/)[1]
如果url有www.something.com,那么我只会收到www请根据您的所有要求更新问题。请尝试
(?:https?:\/\/(?:www\.)?;www\.)([^.\/]+)/
,请参阅。