javascriptreplacegulpgulp-replace

`gulp-replace` is only replacing some, but not all, of the targets


gulp-replace isn't replacing everything that it should.

I'm trying to replace <li>'s and it's only removing some of them. When I add a list item it is successfully removed but the ones in original document are not. This is even true if I copy/paste the <li> into a new file.

test.html is the source document.

<h2>Copied from source:</h2>
<ul>
    <li class="Bullets">Athletics</li>
    <li class="Bullets">Burglary</li>
</ul>

<h2>Entered in Sublime Text 2</h2>
<ul>
    <li>Lorem ipsum dolor sit amet.</li>
    <li>Eos ipsam in dolorum odio!</li>
    <li>Illum asperiores eligendi, iure tempora.</li>
    <li>Reprehenderit blanditiis repudiandae ex eligendi.</li>
</ul>

test.md is what is outputted. Note that the <li>'s that I entered in Sublime Text are removed but the others are not.

<h2>Copied from source:</h2>
<ul>
    <li class="Bullets">Athletics</li>
    <li class="Bullets">Burglary</li>
</ul>

<h2>Entered in Sublime Text 2</h2>
<ul>
    Lorem ipsum dolor sit amet.</li>
    Eos ipsam in dolorum odio!</li>
    Illum asperiores eligendi, iure tempora.</li>
    Reprehenderit blanditiis repudiandae ex eligendi.</li>
</ul>

gulpfile.js

(function () {

  'use strict';

  var gulp = require('gulp');
  var replace = require('gulp-replace');
  var dest = require('gulp-dest');

  gulp.task('replace', function(){
    gulp.src(['source/*.html'])
      .pipe(replace('<li>',''))
      .pipe(dest('dest', {ext: '.md'}))
      .pipe(gulp.dest('./'));
  });

})();

Any help is appreciated! Thank you!


Solution

  • Another regex answer:

    .pipe(replace(/<\s*li[^>]*>(.*?)<\s*\/\s*li>/g, '$1')
    

    Reference