2014-12-19 61 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