【问题标题】:Using Regex in grunt-contrib-copy's "process" option not working在 grunt-contrib-copy 的“进程”选项中使用正则表达式不起作用
【发布时间】:2015-02-19 15:08:22
【问题描述】:

我正在尝试使用 Grunt 的复制插件从我的 index.html 文件中动态删除我的实时重新加载脚本。

我的 Gruntfile 中包含相关代码的部分在这里:

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

我检查了一个正则表达式测试器,它显示我上面的正则表达式应该与我的 html 中的脚本匹配。

虽然正则表达式测试器说它是合法的,但我之前的匹配结果不准确,所以我需要帮助来确定我的 Gruntfile 或正则表达式是否存在问题。有什么建议吗?

【问题讨论】:

    标签: javascript regex gruntjs grunt-contrib-copy


    【解决方案1】:

    您的选项在错误的位置,它们需要更高一级:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 2019-09-23
      • 2020-07-08
      • 2016-09-28
      • 2016-07-31
      相关资源
      最近更新 更多