I have a custom requirement where I need to replace the last occurrence of of a closing brace with empty string,
How can i achieve using Grunt-replace ?
In the below Templates.js file I am replacing
angular.module('abc.templates', []).run(
with
return
Templates.js
Now I have to remove the unnecessary closing brace at line:20 below.
angular.module('abc.templates', []).run(['$templateCache', function($templateCache) {
$templateCache.put("test",
//
//
//
$templateCache.put("test",
//
//
//
$templateCache.put("test",
//
//
//
$templateCache.put("test",
//
//
//
line 20: }]);
In the above Templates.js file I want to remove the last occurence of closing brace ')' at line 20 mentioned above.
Can someone help me with any regex or some other way to achieve this ?
Using grunt-replace you could try the following configuration:
Gruntfile.js
module.exports = function (grunt) {
grunt.initConfig({
replace: {
templateJs: {
options: {
usePrefix: false,
patterns: [
{
match: /angular\.module\('abc\.templates', \[\]\)\.run\(/g,
replacement: 'return '
},
{
match: /(angular\.module\('abc\.templates', \[\]\)\.run\([\w\W]+?}])(\))(;)/g,
replacement: '$1$3'
}
]
},
files: [
// Define your paths as necessary...
{expand: true, flatten: true, src: ['src/template.js'], dest: 'build/'}
]
}
}
});
grunt.loadNpmTasks('grunt-replace');
grunt.registerTask('default', 'replace:templateJs');
};
Additional info
match
regex pattern used in the patterns
Array is explained here . This handles the first part of replacing angular.module('abc.templates', []).run(
with return
match
regex pattern used in the patterns
Array is explained here . This removes the closing brace )
from }]);
to end up with }];
Note : The second regex will match the first instance of the characters }]);
after the initial angular.module('abc.templates', []).run(
part and remove the )
from it. So, unfortunately if the pattern }]);
occurs elsewhere in the body of your function this will not meet your requirement. Otherwise it should be ok.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.