const regex = /(?:(?:http|https):\/\/)?(?:www.|m.)?facebook.com\/(?!home.php)(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\.-]+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:(?:http|https):\\\/\\\/)?(?:www.|m.)?facebook.com\\\/(?!home.php)(?:(?:\\w)*#!\\\/)?(?:pages\\\/)?(?:[?\\w\\-]*\\\/)?(?:profile.php\\?id=(?=\\d.*))?([\\w\\.-]+)', 'gm')
const str = `http://www.facebook.com/jquery2dotnet
http://www.facebook.com/some.user
https://www.facebook.com/#!/my_page_id
http://www.facebook.com/pages/test
http://www.facebook.com/pages/test/123
https://www.facebook.com/#!/page_with_1_number
http://www.facebook.com/bounce_page#!/pages/test-Url/12345
https://www.facebook.com/bounce_page#!/my_page_id?v=app_1123325
https://www.facebook.com/notes/hyperarts-web-design
http://www.facebook.com/test.username
https://www.fb.me/jquery2dotnet
http://www.facebook.com/
http://m.facebook.com/home.php
http://m.facebook.com/test.username
https://mbasic.facebook.com/home.php?_rdr`;
// 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