const regex = /<img\s[^>]*src="(?<imageURL>[^"]*)"\s*\/> # IMG tag
.*? # Anything in-between IMG and A
<a\s[^>]*?href="\/title\/tt
(?<imdbid>\d{7}) # Got the imdbid
\/[^>]*>(?<title>.*?) # Got title
<\/a> # End of A tag
\s*\(
(?<year>\d{4}) # Year
\)\s*(?:\( # Type is optional
(?<type>[^<]*) # Type
\))? # End of optional group/igs;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('<img\\s[^>]*src="(?<imageURL>[^"]*)"\\s*\\\/> # IMG tag
.*? # Anything in-between IMG and A
<a\\s[^>]*?href="\\\/title\\\/tt
(?<imdbid>\\d{7}) # Got the imdbid
\\\/[^>]*>(?<title>.*?) # Got title
<\\\/a> # End of A tag
\\s*\\(
(?<year>\\d{4}) # Year
\\)\\s*(?:\\( # Type is optional
(?<type>[^<]*) # Type
\\))? # End of optional group', 'igs')
const str = `<tr class="findResult odd">
<td class="primary_photo"><a href="/title/tt0499549/?ref_=fn_tt_tt_1" ><img src="http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX32_CR0,0,32,44_AL_.jpg" /></a></td>
<td class="result_text"><a href="/title/tt0499549/?ref_=fn_tt_tt_1" >Avatar</a> (2009) </td>
</tr>
<tr class="findResult even">
<td class="primary_photo"><a href="/title/tt0417299/?ref_=fn_tt_tt_2" ><img src="http://ia.media-imdb.com/images/M/MV5BMTM3MTc3OTc0NF5BMl5BanBnXkFtZTcwOTQ0OTM1MQ@@._V1._CR34,0,295,440_SX32_CR0,0,32,44_AL_.jpg" /></a></td>
<td class="result_text"><a href="/title/tt0417299/?ref_=fn_tt_tt_2" >Avatar: The Last Airbender</a> (2005) (TV Series) </td>
</tr>`;
// 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