const regex = /<caption>((?:.)*?)<\/caption>/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('<caption>((?:.)*?)<\\\/caption>', 'g')
const str = `<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">
<caption>Турнірна таблиця групи «J»:</caption>
<tbody>
<tr>
<td>№</td>
<td>Команда</td>
<td>І</td>
<td>В</td>
<td>Н</td>
<td>П</td>
<td>З:П</td>
<td>О</td>
</tr>
<tr>
<td>1.</td>
<td>«Естерсунд» (Швеція)</td>
<td>3</td>
<td>2</td>
<td>1</td>
<td>0</td>
<td>5:2</td>
<td>7</td>
</tr>
<tr>
<td>2.</td>
<td><strong>«ЗОРЯ» (Україна)</strong></td>
<td>3</td>
<td>2</td>
<td>0</td>
<td>1</td>
<td>3:3</td>
<td>6</td>
</tr>
<tr>
<td>3.</td>
<td>«Атлетік» (Іспанія)</td>
<td>3</td>
<td>0</td>
<td>2</td>
<td>1</td>
<td>2:3</td>
<td>2</td>
</tr>
<tr>
<td>4.</td>
<td>«Герта» (Німеччина)</td>
<td>3</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>1:3</td>
<td>1</td>
</tr>
</tbody>
</table>`;
// 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