Grunt:替换特定的单词

时间:2014-12-19 09:28:56

标签: gruntjs str-replace

下面是一个给定的类,我只想替换特定的行。

class abc{

  Rest of code...

  Titan.include('`MyFunction/myControl`.js');

  Rest of code...

}

我想将Titan.include替换为myfunc并删除" .js"从行:

myfunc('`MyFunction/myControl`');

我正在使用grunt.loadNpmTasks('grunt-string-replace');

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果您使用grunt-text-replace插件,您可以按以下方式配置它:

replace: {
      myReplace: {
        src: [ 'yourFile' ], //your file
        dest: 'destinationFile', //path and namefile where you will save the changes.
        replacements: [ {
          from: /Titan\.include\('(.*)\.js'\);/g,   //Regexp to match what you need
          to: "myfunc('$1');"   //Replacement for the Regexp
        } ]
      }
    }

无论如何,我从来没有使用过grunt-string-replace插件,但是如果你需要使用它而不是grunt-text-replace我应该以这种方式工作:

'string-replace': {
  dist: {
    files: {
      'dest/': 'yourfile' //Key: path  where you will save the changes. Value: File or path where you will search.
    },
    options: {
      replacements: [{
        pattern: /Titan\.include\('(.*)\.js'\);/g,
        replacement: "myfunc('$1');"
      }]
    }
  }