how to split any url and get only source?

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?

7 thoughts on “how to split any url and get only source?”

  1. Do you accept Regex response?

    const url='https://abcnews.go.com/'
    let subdomain = url.match(/(\w*)\./)[1]
    // 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).

    Reply

Leave a Comment