Search code examples
htmlregexmeta-tags

Regex to match meta description tags that are containing non white space characters


Hi you smart RegEx Magicians out there!

I am trying to create a RegEx that matches the meta description tag(s) in my source code that are not empty or not just containing white spaces.

So e.g. this should match:

<meta name="description" content="This is my super great meta description &amp; I love it so much!"/>
<meta name="description" content=" This is my super great meta description &amp; I love it so much!"/>
<meta name="description" content="This is my super great meta description &amp; I love it so much! "/>

This e.g. shouldn't match:

<meta name="description" content=""/>
<meta name="description" content=" "/>
<meta name="description" content="  "/>
<meta name="description" content="
"/>

So far I have come up with this, but of course that is not enough and I don't really know how to replace the wildcard...

#<\s*meta[^>]\s*name\s*=\s*("|')description("|')\s*content\s*=\s*("|').*("|')?\/?[^>]*>#i

Anyone who can help?

Much love and appreciation!


Solution

  • Your regex seems overly complex and not right.

    You can use this (?!\s*") negative look ahead to reject the match if content attribute just contains zero or more whitespace characters. Over all regex would be this,

    <meta\s+name="description"\s+content="(?!\s*").*"\/>
    

    Demo