const regex = /facebook.*cpc/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('facebook.*cpc', 'gm')
const str = `free.facebook.com
web.facebook.com
facebook/social/listing
facebook/referral/[facebook][marketplace][house][rent]
facebook/referral/[facebook][marketplace][townhouse][rent]
facebook/cpc/leadgen
facebook/cpc/[facebook][cpc][house][sale]
facebook/cpc/[facebook][cpc][condo][sale]
facebook/cpc/Aus + NZ, Men Over 50s, int Thailand/New Luxury Retirement Village in Southern Thailand
facebook/cpc/[facebook][cpc][townhouse][sale]
facebook/cpc/[facebook][cpc][condo][rent]
facebook/cpc/client->-developer->-leadgen
facebook/cpc/event/
facebook/cpc/[facebook][cpc][land][sale]`;
// 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