Bhavin2887
Bhavin2887

Reputation: 180

Grunt : Replace specific words in line

Below is a class given from which i want to replace only specific lines.

class abc{

  Rest of code...

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

  Rest of code...

}

I want to replace Titan.include with myfunc and remove the word ".js" from the line:

myfunc('`MyFunction/myControl`');

I am using grunt.loadNpmTasks('grunt-string-replace');

Any help is appreciated.

Upvotes: 1

Views: 654

Answers (1)

Carlos Lancha
Carlos Lancha

Reputation: 36

If you use grunt-text-replace plugin you can configure it in the following way:

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
        } ]
      }
    }

Anyway, I never used the grunt-string-replace plugin, but if you NEED to use that instead grunt-text-replace I thing should work in this way:

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

Upvotes: 2

Related Questions