const regex = /^http[s]?:\/\/www\.linkedin\.com\/(?:in|pub|public-profile\/in|public-profile\/pub)\/(?:[\w]{6}-[\w]{1,}-[\w]+)$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^http[s]?:\\\/\\\/www\\.linkedin\\.com\\\/(?:in|pub|public-profile\\\/in|public-profile\\\/pub)\\\/(?:[\\w]{6}-[\\w]{1,}-[\\w]+)$', 'gm')
const str = `https://www.linkedin.com/in/XXXXXX-XXXXX-55301b41
https://www.linkedin.com/pub/XXXXXX-XXXXX-55301b41
https://www.linkedin.com/public-profile/in/XXXXXX-XXXX-b82a7b10a
https://www.linkedin.com/public-profile/pub/XXXXXX-XXXX-b82a7b10a
https://www.linkedin.com/in/xxxx-xxxx-ab85a328/%7Bcountry%3Dde%2C+language%3Dde%7D?trk=people-guest_profile-result-card_result-card_full-click
https://www.linkedin.com/edu/school?id=18987
https://www.linkedin.com/in/xxxx-xxxx-55301b41/de
https://www.linkedin.com/pub/xxxxx-xxxxx/98/b7a/22b
https://it.linkedin.com/public-profile/in/xxxx-xxxxx-80520667?challengeId=AQEV3tHveORYsAAAAXQqx1N3bZ7fd44s5ngegJp6rC0UoWvhG2LtAhhgld8h3QyBxorfcLL2iuvK4xh_UKoGguxvX6sFx_nnZA&submissionId=7bc972ed-bfd1-2e16-c395-ad35cb550117
`;
// 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