Regex To Match Content Between HTML Tags

A regular expression to match all text content between the opening and closing HTML tags. Supports both single lines and multiple lines.

Don’t forget to replace the HTML tag name (div in this example) according to your actual needs before you start using this regular expression.

/(?<=(<div>))(\w|\d|\n|[().,\-:;@#$%^&*\[\]"'+–/\/®°⁰!?{}|`~]| )+?(?=(</div>))/g

Matches:

  • <div>RegexPattern</div>
  • <div>RegexPattern</div><div>RegexPattern</div>

Non-matches:

  • <div>RegexPattern<div>
  • <p>RegexPattern</p>

Note that you might find the Regex listed above only matches content between HTML tags.  To include the wrapping tags, use this Regex instead:

/<div.*>.*?<\/div>/ig

Matches:

  • <div>RegexPattern</div>
  • <div>RegexPattern</div><div>RegexPattern</div>

See Also:

Regex Is Copied!