I have a difficulty building a regex.
Suppose there is a html clip as below.
I want to use Javascript to cut the <tbody>
part with the link of "apple"(which <a>
is inside of the <td class="by">
)
I construct the following expression :
/<tbody.*?text[\s\S]*?<td class="by"[\s\S]*?<a.*?>apple<\/a>[\s\S]*?<\/tbody>/g
But the result is different from what I wanted. Each match contains more than one block of <tbody>
. How it should be? Regards!!!!
(I tested with https://regex101.com/ and get the unexpected selection. Please forgive me I can't figure out the problem :( )
<tbody id="text_0">
<td class="by">
...lots of other tags
<a href="xxx">cat</a>
...lots of other tags
</td>
</tbody>
<tbody id="text_1">
...lots of other tags
<td class="by">
<a href="xxx">apple</a>
</td>
...lots of other tags
</tbody>
<tbody id="text_2">
...lots of other tags
<td class="by">
<a href="xxx">cat</a>
</td>
...lots of other tags
</tbody>
<tbody id="text_3">
...lots of other tags
<td class="by">
...lots of other tags
<a href="xxx">tiger</a>
</td>
...lots of other tags
</tbody>
<tbody id="text_4">
<td class="by">
<a href="xxx">banana</a>
</td>
</tbody>
<tbody id="text_5">
<td class="by">
<a href="xxx">peach</a>
</td>
</tbody>
<tbody id="text_6">
<td class="by">
<a href="xxx">apple</a>
</td>
</tbody>
<tbody id="text_7">
<td class="by">
<a href="xxx">banana</a>
</td>
</tbody>
And this is what i expect to get
<tbody id="text_1">
<td class="by">
<a href="xxx">apple</a>
</td>
</tbody>
<tbody id="text_6">
<td class="by">
<a href="xxx">apple</a>
</td>
</tbody>