Matching html tags with regex in vscode

Viewed 616

I have the regex <meta[^>]*content="[^"]+"> and would like to use it with vscode search and replace to remove the content attribute from the meta tags. It works fine when the tag is on one line only:

<meta http-equiv="X-UA-Compatible" content="IE=edge">

The problem is that it doesn't work for tags that span multiple lines like:

<meta http-equiv="X-UA-Compatible" 
    content="IE=edge">

Can i modify it to also work with multiline tags?

2 Answers

You can try this <meta([^>]*)([\n\s\S])content="[^"]+">.

I changed your regex by adding [\n\s\S] group that matches for any new line or any content

Notice: You have to use the current file search with CTRL+F / CMD+F to use the following. Global file search with CTRL+SHIFT+F / CMD+SHIFT+F does not work due to lookbehind assertion without fixed length!

You can use the following regex for searching:

(?<=<meta.*?)[\s\n]+content="[^"]+"

What it does:

[\s\n]+content="[^"]+" : Match all content attributes with its preceding white spaces and new lines
(?<=<meta.*?) : Look behind for opening meta tags and optionally other attributes in between

Then simply replace with an empty string and your content attributes + preceding white spaces/new lines are gone.

Tested with

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta http-equiv="X-UA-Compatible" 
    content="IE=edge">

<meta content="IE=edge">

<meta http-equiv="X-UA-Compatible" 
    content="IE=edge"
    name="test">

<meta http-equiv="X-UA-Compatible" 
    content="IE=edge" name="test">

<link content="IE=edge">
Related