Вы можете использовать регулярное выражение /(.+)\/@([^\/\r\n]+)/
для захвата как сайта, так и имени пользователя следующим образом:
var re = /(.+)\/@([^\/\r\n]+)/;
var match = re.exec(url);
if (match != null) {
site = match[1];
user = match[2];
}
Используя ваши примеры:
'https://site1.com/@bob' --> site = "https://site1.com"; user = "bob"
'https://site2.com/boats/@frank/how-to-fix-your-boat' --> site = "https://site2.com/boats"; user = "frank"
'http://site2.com/@frank/settings' --> site = "http://site2.com"; user = "frank"
'site1.com/@joe.beans/re-how-to-fix-your-boat-248435252' --> site = "site1.com"; user = "joe.beans"
EDIT
Если вы хотите захватить протокол, домен и пользователя, то это должно сделать это:
var re = /^((?:http|ftp)s?:\/\/)?(?:www\.)?([^@\/\r\n]+)?(?:\/.+)?\/@([^\/\r\n]+)/;
var match = re.exec(url);
if (match != null) {
protocol = match[1];
domain = match[2];
user = match[3];
}
Это даст:
url match[1] match[2] match[3]
--- -------- -------- --------
https://site1.com/@bob --> https:// site1.com bob
https://site2.com/boats/@frank/how-to-fix-your-boat --> https:// site2.com frank
http://site2.com/@frank/settings --> http:// site2.com frank
site1.com/@joe.beans/re-how-to-fix-your-boat-248435252 --> site1.com joe.beans
Regex Details
"^" + Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed, line feed, line separator, paragraph separator)
"(" + Match the regex below and capture its match into backreference number 1
"(?:" + Match the regular expression below
Match this alternative (attempting the next alternative only if this one fails)
"http" + Match the character string “http” literally (case insensitive)
"|" +
Or match this alternative (the entire group fails if this one fails to match)
"ftp" + Match the character string “ftp” literally (case insensitive)
")" +
"s" + Match the character “s” literally (case insensitive)
"?" + Between zero and one times, as many times as possible, giving back as needed (greedy)
":" + Match the character “:” literally
"\\/" + Match the character “/” literally
"\\/" + Match the character “/” literally
")" +
"?" + Between zero and one times, as many times as possible, giving back as needed (greedy)
"(?:" + Match the regular expression below
"www" + Match the character string “www” literally (case insensitive)
"\\." + Match the character “.” literally
")" +
"?" + Between zero and one times, as many times as possible, giving back as needed (greedy)
"(" + Match the regex below and capture its match into backreference number 2
"[^" + Match any single character NOT present in the list below
"@" + The literal character “@”
"\\/" + The literal character “/”
"\r" + The carriage return character
"\n" + The line feed character
"]" +
"+" + Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")" +
"?" + Between zero and one times, as many times as possible, giving back as needed (greedy)
"(?:" + Match the regular expression below
"/" + Match the character “/” literally
"." + Match any single character that is NOT a line break character (line feed, carriage return, line separator, paragraph separator)
"+" + Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")" +
"?" + Between zero and one times, as many times as possible, giving back as needed (greedy)
"/@" + Match the character string “/@” literally
"(" + Match the regex below and capture its match into backreference number 3
"[^" + Match any single character NOT present in the list below
"\\/" + The literal character “/”
"\r" + The carriage return character
"\n" + The line feed character
"]" +
"+" + Between one and unlimited times, as many times as possible, giving back as needed (greedy)
")"