Regular match for each subdomain URL

I am trying to write a regex that extracts part of a subdomain / domain URL as separate lines.

I tried this:

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

It should work against these URLs:

http;//www.mail.yahoo.co.uk/blah/blah

http;//test.test.again.mail.yahoo.com/blah/blah

I want to break it into pieces like this:

["http://", "www", ".mail", ".yahoo", ".co", ".uk"]

["http://", "test", ".test", ".again", ".mail", ".yahoo", ".com"]

Now I can only capture them as:

["http://", "www", ".uk"]

["http://", "test", ".com"]

Does anyone know how I can fix my regular expression?

+4
source share
3 answers

You can use /(http[s]?:\/\/|\w+(?=\.)|\.\w+)/g. Check it out online

+1
source

You can use regex

(^\w+:\/\/)([^.]+)

to match the first part and then use

\.\w+

to match the second part

check code snippet

function getSubDomains(str){
    let result = str.match(/(^\w+:\/\/)([^.]+)/);
    result.splice(0, 1);
    result = result.concat(str.match(/\.\w+/g));
    console.log(result);
    return result;
}

getSubDomains('http://www.mail.yahoo.co.uk/blah/blah');
getSubDomains('http://test.test.again.mail.yahoo.com/blah/blah');
Run code
0
source

y

var str = 'http://test.test.again.mail.yahoo.com/blah/blah';

var res = str.match(/^[a-z]+:\/\/|\.?[^/.\s]+/yig);

console.log(res);

See Regex101 for details.

0
source

Source: https://habr.com/ru/post/1684009/


All Articles