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

与世无争的帅哥 提交于 2019-12-13 05:46:15

问题


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, " ");
                }
            }


来源:https://stackoverflow.com/questions/26190445/using-regex-in-grunt-contrib-copys-process-option-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!