const regex = /magnet:\?((&?xt\.?\d*=urn:[A-Za-z0-9][A-Za-z0-9-]{0,31}:[A-Fa-f0-9]{20,50})|(&?(dn|tr|xl|ws|as|xs|kt|mt)=[A-Za-z0-9\-._%+;=:?\/]+)&?)+$/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('magnet:\\?((&?xt\\.?\\d*=urn:[A-Za-z0-9][A-Za-z0-9-]{0,31}:[A-Fa-f0-9]{20,50})|(&?(dn|tr|xl|ws|as|xs|kt|mt)=[A-Za-z0-9\\-._%+;=:?\\\/]+)&?)+$', 'g')
const str = `magnet:?xt=urn:btih:6B5FFA38E0C811E0738697BEC13DDDA57A60FCB4&dn=Taboo.UK.Season.1.S01.1080p.AMZN.WEBRip.5.1.HEVC.x265-GIRAYS&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2920%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&as=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounc/e&tr=udp%3A%2F%2Ftracker.pirateparty.gr%3A6969%2Fannounce&tr=udp%3/A%2F%2Ftracker.cyberia.is%3A6969%2Fannounce&xt=urn:btih:6B5FFA38E0C811E0738697BEC13DDDA57A60FCB4&xt=urn:btih:6B5FFA38E0C811E0738697BEC13DDDA57A60FCB4&xs=dchub://example.org&as=http%3A%2F%2Fdownload.wikimedia.org%2Fmediawiki%2F1.15%2Fmediawiki-1.15.1.tar.gz&xl=10826029&dn=mediawiki-1.15.1.tar.gz`;
// 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