我有一个自定义的要求,在这里我需要用空字符串替换结束大括号的最后一次出现,
我怎样才能实现使用Grunt-替换?
在下面的Templates.js文件中,我要替换
angular.module('abc.templates', []).run(
with
returnTemplates.js
现在,我必须删除不必要的结束支撑线:以下20。
angular.module('abc.templates', []).run(['$templateCache', function($templateCache) {
$templateCache.put("test",
//
//
//
$templateCache.put("test",
//
//
//
$templateCache.put("test",
//
//
//
$templateCache.put("test",
//
//
//
line 20: }]);在上面的Templates.js文件中,我想移除上面提到的第20行中最后出现的关闭大括号‘)。
有人能帮我做任何调整或其他方法来实现这一点吗?
发布于 2017-09-07 11:52:00
使用咕噜-替换,您可以尝试以下配置:
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');
};附加信息
match数组中使用的第一个patterns正则表达式模式( 这里 )。这将处理用angular.module('abc.templates', []).run(替换return的第一部分。match数组中使用的第二个patterns正则表达式模式( 这里 )。这将从)中移除关闭的大括号}]);,从而以}];结束。注意事项:第二个正则表达式将匹配初始angular.module('abc.templates', []).run(部件之后的字符}]);的第一个实例,并从其中删除)。因此,不幸的是,如果模式}]);发生在函数主体的其他地方,这将不能满足您的要求。否则一切都会好起来的。
https://stackoverflow.com/questions/46078362
复制相似问题