I want to split https://abcnews.go.com/ this type of URL.
Want to get abcnews
I used:
.split('//')[1]
.split('www.')[1]
.split('.com')[0]
.split('.org')[0]
.split('.edu')[0]
.split('.info')[0]
I get error can not split.
Any idea how to do it?
I want to split https://abcnews.go.com/ this type of URL.
Want to get abcnews
I used:
.split('//')[1]
.split('www.')[1]
.split('.com')[0]
.split('.org')[0]
.split('.edu')[0]
.split('.info')[0]
I get error can not split.
Any idea how to do it?
This looks safe:
(?:https?:\/\/(?:www\.)?|www\.)([^.\/]+)
See proof.
EXPLANATION
--------------------------------------------------------------------------------
(?: 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 code:
const url = "https://abcnews.go.com/"
const regex = /(?:https?:\/\/(?:www\.)?|www\.)([^.\/]+)/
const match = url.match(regex)
if (match) {
console.log(match[1])
}
Do you accept Regex's response?
const url='https://abcnews.go.com/';
let subdomain = url.match(/(\w*)\./)[1];
console.log(subdomain)
// subdomain = "abcnews"
With that, you get the word before the first dot.
I think you get an error after:
.split('//')[1].split('www.')[1]
Because you have no "www", then the length of the array is 1 (array[0] only).
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"