-->

Using Regex in grunt-contrib-copy's “process”

2019-08-29 23:33发布

问题:

I'm trying to remove my live reload script from my index.html file dynamically using Grunt's copy plugin.

The part of my Gruntfile with the code in question is here:

copy: {
            main: {
                files: [
                {
                    expand: true, 
                    src: 'index.html',
                    dest: 'build/',
                    options: {
                        process: function (content, srcpath){
                            return content.replace(/<script src = "http:\/\/localhost:9090\/livereload.js"><\/script>/g, " ");
                        }
                    }
                },

I checked a regex tester and it showed that the regular expression I have above should match the script in my html.

Although the regex tester says it is legit, I have had inaccurate results with matches before, so I need help here in determining if there is a problem with my Gruntfile, or with my regular expression. Any suggestions?

回答1:

your options are in the wrong place, they need to be one level higher:

copy: {
        main: {
            files: [
            {
                expand: true, 
                src: 'index.html',
                dest: 'build/'
            },
            options: {
                process: function (content, srcpath){
                    return content.replace(/<script src = "http:\/\/localhost:9090\/livereload.js"><\/script>/g, " ");
                }
            }