Regex Javascript - Remove text between two html comments -


well have 2 html comments this:

<!--delete--> blah blah blah blah <!--delete--> 

and want remove (including comments, character , newlines). btw using javascript , grunt make replacement.

thanks

regex

use following javascript regular expression match multiple instances of custom .html comments , content inside them:

/\<\!\-\-delete\-\-\>((.|[\n|\r|\r\n])*?)\<\!\-\-delete\-\-\>[\n|\r|\r\n]?(\s+)?/g 

then register custom function task inside gruntfile.js shown in following gist:

gruntfile.js

module.exports = function (grunt) {      grunt.initconfig({         // ... other tasks     });      grunt.registertask('processhtmlcomments',         'remove content inside custom delete comments',         function() {             var srcdocpath = './src/index.html', // <-- define src path .html                 outputdocpath = './dist/index.html',// <-- define dest path .html                  doc = grunt.file.read(srcdocpath, {encoding: 'utf8'}),                 re = /\<\!\-\-delete\-\-\>((.|[\n|\r|\r\n])*?)\<\!\-\-delete\-\-\>[\n|\r|\r\n]?(\s+)?/g,                 contents = doc.replace(re, '');              grunt.file.write(outputdocpath, contents, {encoding: 'utf8'});             console.log('created file: ' + outputdocpath);         });      grunt.registertask('default', [         'processhtmlcomments'     ]);  }; 

additional notes

currently running $ grunt via cli following:

  1. reads file named index.html src folder.
  2. deletes content inside starting , closing custom comments, <!--delete-->, including comments themselves.
  3. writes new index.html, excluding unwanted content, dist folder.

both values srcdocpath , outputdocpath need redefined per projects requirements.


edit updated regex allow inline comment usage. example:

<p>this text remains <!--delete-->i deleted<!--delete-->blah blah</p> 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -