const regex = /^(?<title>.+?)[_.\s-]+(?<season>S\d+)(?:[.\-\s_]*?(?<seasonmax>S?\d+))?(?=[_.\s](?!E\d+))/img;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<title>.+?)[_.\\s-]+(?<season>S\\d+)(?:[.\\-\\s_]*?(?<seasonmax>S?\\d+))?(?=[_.\\s](?!E\\d+))', 'img')
const str = `daily.show.name.2023.04.18.guest.name.1080p.web.h264-jebaited.mkv
BBC.News.at.Ten.2023.10.11.1080i.HDTV.DD2.0.H.264-BLS
WWE.NXT.2023.10.10.720p.WEB.h264-SPORTSNET
Tesco.How.Do.They.Really.Do.It.2023.1080p.HDTV.H264-DARKFLiX
Seth.Meyers.2023.10.09.Amy.Sedaris.1080p.WEB.h264-EDITH
NFL.2023.10.09.49ers.Vs.Cowboys.UNCUT.1080p.WEB.H264-SPORTSNET
Neighbours.S39E012.2023-10-05.Episode.8915.1080p.AMZN.WEB-DL.DDP2.0.H.264-SDCC
DFL.Supercup.2023 08 12.Bayern.Munchen.vs.RB.Leipzig.HDTV.2160p.AAC2.0.HEVC.mkv
The Late Show with Stephen Colbert (2015) - 2023-10-05 - Bob Odenkirk Fortune Feimster [2.0 WEBDL-1080p][x264]-EDITH.mkv
Anger Management (2012) - S02E15 - Charlies Patients Hook Up [WEBDL-1080p][AC3 5.1][h264]-BS.mkv
Ahsoka (2023) - S01E01 - Part One Master and Apprentice [DSNP WEBDL-1080p][EAC3 Atmos 5.1][h264]-NTb.mkv'
Loki - S01E01 - Glorious Purpose [DSNP WEBDL-1080p Proper][EAC3 Atmos 5.1][h264]-TOMMY.mkv'
Family - Guy (1999) - S01E01 - Death Has a Shadow [DSNP WEBDL-1080p][AAC 2.0][h264]-PHOENiX.mkv'
Neighbours.S39E012.2023-10-05.Episode.8915.1080p.AMZN.WEB-DL.DDP2.0.H.264-SDCC
Rick and Morty (2013) - S06E06 - JuRicksic Mort [HMAX DD 5.1 WEBDL-1080p][x264]-NTb.mkv
1923.S01E01.1080p.BluRay.TrueHD5.1.H.264-SLIPSTREAM
11.22.63.S01E01.2160p.WEB-DL.DDP5.1.H.265-NiXON
1923.S01.1080p.BluRay.TrueHD5.1.H.264-SLIPSTREAM
11.22.63.S01.2160p.WEB-DL.DDP5.1.H.265-NiXON
Hello.World.S01.ntb.mkv
Hello.World.S01 - S03.ntb.mkv
Hello.World.S1.ntb.mkv
Hello.World.S01 S03.ntb.mkv
Hello.World.S01.Extras.ntb.mkv
Hello_World_S01_S02.ntb.mkv
Hello_World_S01-S03_ntb.mkv
Hello_World_S1_ntb.mkv
Hello.World.S1E03.ntb.mkv
Hello.World.S031 E01.ntb.mkv
Hello_World_S01E01_ntb.mkv
Hello_World_S1E03_ntb.mkv
Hello.World.S01.E01.ntb.mkv
Hello_World_S01_E01_ntb.mkv`;
// 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