2014-12-19 88 views
0

下面是一个给出的类,我只想替换特定的行。Grunt:替换行中的特定单词

class abc{ 

    Rest of code... 

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

    Rest of code... 

} 

我想替换Titan.includemyfunc并从线中删除 “的.js”:

myfunc('`MyFunction/myControl`'); 

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

任何帮助表示赞赏。

+1

好的,那么你有什么尝试?是否有任何错误?我们没有看到问题所在。 – jakerella 2014-12-19 15:50:10

+0

@jakerella我试过下面的东西。但它只能替换文件的第一行,其他行也是一样的。 DIST:{ 文件:[{ 扩大:真实, CWD: '资源/', SRC: '**/* JS。', DEST: '资源/' }], 选项:{ 更换:[{ 图案: 'Titan.include', 替换: 'MYFUNC' }] } } – Bhavin2887 2014-12-22 05:57:50

回答

1

如果使用咕噜文本替换插件,您可以通过以下方式进行配置:

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-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');" 
     }] 
    } 
    } 
+0

文件:[{ 扩大:真, CWD: '资源/', SRC:“**/* .js', dest:'Resources /' }]这些参数在文件中丢失 – Bhavin2887 2014-12-22 06:44:11