const regex = /^(?:https?:\/\/)((?:www\.)|(?:m\.))?soundcloud\.com\/[a-z0-9](?!.*?(-|_){2})[\w-]{1,23}[a-z0-9](?:\/.+)?$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?:https?:\\\/\\\/)((?:www\\.)|(?:m\\.))?soundcloud\\.com\\\/[a-z0-9](?!.*?(-|_){2})[\\w-]{1,23}[a-z0-9](?:\\\/.+)?$', 'gm')
const str = `Use only numbers, lowercase letters, underscores, or hyphens.
Profile URLs must not start or end with an underscore or hyphen.
Profile URLs must not have two consecutive underscores or hyphens.
Profile URLs must be between 3 and 25 characters.
http://www.soundcloud.com/fo
http://www.soundcloud.com/foo
https://m.soundcloud.com/abcd123
https://soundcloud.com/abcd123
https://soundcloud.com/
https://m.soundcloud.com/abcd123_l
https://soundcloud.com/abcd123-l
http://www.soundcloud.com/-abcd123
http://www.soundcloud.com/_abcd123
https://m.soundcloud.com/abcd123-_l
https://m.soundcloud.com/abcd123--l
https://m.soundcloud.com/1234567890123456789012345
https://m.soundcloud.com/12345678901234567890123456
https://m.soundcloud.com/1234567890123456789012345/fooo12;lkflkjlfjkljskljflks/
https://m.soundcloud.com/12345678901234567890123456/fooo
http://www.m.soundcloud.com/abcdef`;
// 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