SoFunction
Updated on 2025-03-10

Regular expression matching everything in form form

The demand is like this:
Now there is a form

Copy the codeThe code is as follows:
<form action=''>.......</form>

The ellipsis in the form represents the content inside, and there are various tags <>.
I want to write a regular expression to match the entire form form, including the form tag, which is the content in the html file.

The first thing that comes to mind is:

Copy the codeThe code is as follows:

<form .*</form>

This way:
Copy the codeThe code is as follows:

<form [^>]*/form>

And this is also true:
Copy the codeThe code is as follows:

<form .*<\/form>

As long as there is a tag in the form, the content will not be matched. It's inexplicable and I don't know why.

Later, I asked a master of regular expressions and he told me another way of writing:

Copy the codeThe code is as follows:

<form [\s\S]*</form>

This involves greedy and non-greedy modes.

[\s\S] means to match all whitespace characters + non-whitespace characters. To put it bluntly, all characters are OK
* means 0 or more, and it stops until it encounters the one behind.

This matches all the contents in the form.

Note: The editor used <form [\s\S]*</form> to verify it in DW.