我想拆分https://abcnews.go.com/
这种类型的URL。
想要获得abcnews
我用过:
.split('//')[1]
.split('www.')[1]
.split('.com')[0]
.split('.org')[0]
.split('.edu')[0]
.split('.info')[0]
我得到错误不能分开。
知道怎么做吗?
这看起来很安全:
(?: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代码:
const url = "https://abcnews.go.com/"
const regex = /(?:https?://(?:www.)?|www.)([^./]+)/
const match = url.match(regex)
if (match) {
console.log(match[1])
}
您接受Regex的回复吗?
const url='https://abcnews.go.com/';
let subdomain = url.match(/(w*)./)[1];
console.log(subdomain)
// subdomain = "abcnews"
这样,你就可以在第一个点之前得到单词。
我认为您在以下时间后会出现错误:.split('//')[1].split('www.')[1]
因为你没有;www";,则数组的长度为1(仅array[0])。
const rgx=/(?:www.)?(w*)./;
const abc='https://abcnews.go.com/';
const www='https://www.google.com/';
console.log(abc.match(rgx)[1]);
// "abcnews"
console.log(www.match(rgx)[1]);
// "google"