Finnegan Finnegan - 3 months ago 8
HTML Question

How can I make regex ignore content inbetween sets of HTML tags?

In this situation:

<script>
function();
</script>

please don't touch me.

<script>
function();
</script>


I am trying to capture the tags and the information between them, but not the "please don't touch me in the middle". So far I have come up with this:

/<script>[^]+<\/script>/ig


Which captures everything from the very first
<script>
tag to the very last
</script>
tag.

Answer

You need to make the regex not greedy, since it's greedy by default:

<script>[^]+?<\/script>