drogon drogon - 2 months ago 21
HTML Question

Regex match multiple meta tags in multi line string

I want to validate that a multi line string contains only meta tags in the format I specify. For example it can only contain one or more of these:

<meta property="og:[anything]" content="[anything]"/>


Where [anything] can be 0 or more of any character.

The meta tags may or may not me separated by new lines, so this is also ok:

<meta property="og:[anything]" content="[anything]"/>
<meta property="og:[anything]" content="[anything]"/>
<meta property="og:[anything]" content="[anything]"/>


Here is my current regex:

^(?:<meta [^>]*property=[\"']og:.+[\"'] [^>]*content=[\"']([^'^\"]+?)[\"'][^>]*>)(?:\n*)$


It works if the tags are on the same line, or if there's a tag with a newline, but not for tags on multiple lines.

How can I get it to match only these meta tags either on line or multiple lines?

Answer

How about ^(?:\s*<meta\s*property=["']og:.+?["']\s*content=["'].+?["']\s*\/>\s*)+$

See: https://regex101.com/r/iJ7fT8/2