const regex = /(?<=profile\.php\?id\=)(?<id>[0-9]+)|(?:(?<=\.com)|(?<=\.me)|(?<=\.co)|(?<=\.us))(?:(?:\/groups\/|\/)(?!profile\.php)(?<username>[\w\.\_]+))/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<=profile\\.php\\?id\\=)(?<id>[0-9]+)|(?:(?<=\\.com)|(?<=\\.me)|(?<=\\.co)|(?<=\\.us))(?:(?:\\\/groups\\\/|\\\/)(?!profile\\.php)(?<username>[\\w\\.\\_]+))', 'gm')
const str = `https://www.facebook.com/hung.le054
https://www.facebook.com/groups/j2team.community/?ref=nf_target&fref=nf&__xts__%5B0%5D=68.ARCufAkDYLfgBw09Ctl9HKUd8wMYIaATKYHE-9guWWpCGu86iH8k0p4Qx8BxlXEEJMW8shzzodIckx0qvnBg94kwMe3uOIPnjE9s6neoXwFX8kBJsWBpF-Ej8oXpGdiIu6sf9cg9tyeq&__tn__=C-R
https://www.facebook.com/NoExit.GoEXID/?hc_ref=ARQCFBLI4l-ciKCPEGazrn4_8WTxAulTr9a7XgMTgi_OKA_Iad1-hMzdD2z0snaq3BE&fref=nf&__xts__%5B0%5D=68.ARAUlT1DqV2wkn3VWI7belmtRn5k9XXk05WUw1Gl88lm2CDjGSmYX0zI_p8A19EG2w6iWF3t0gZN-GurkpGl05KGdS5Xv8lC__sOmlxD8Jz5CrhOjbz1ThvYtiZA3yzk4P66-V5BjDty&__tn__=kCH-R
https://www.facebook.com/profile.php?id=100010004806283&fref=pb&hc_location=friends_tab`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions