bomsy / grunt-regex-replace Goto Github PK
View Code? Open in Web Editor NEWGrunt plugin to search and replace text content of files based on regular expression patterns
License: MIT License
Grunt plugin to search and replace text content of files based on regular expression patterns
License: MIT License
Add option for each way to keep matching the search expression in a file and action each found.
Maybe I am overlooking some usage that will replace all matches in a file, including multiple matches on a single line.
But it seems to only replace the first match, then reset the file back to start,
Any chance a destination can be added so it doesn't overwrite the original file it is modifying?
It looks like the options flags are never applied as part of the replace
Right know if one install grunt-regex-replace with grunt 1.0.1 get an error:
The package [email protected] does not satisfy its siblings' peerDependencies requirements!
This is caused by the strict requirement for grunt 0.4.0 specified in https://github.com/bomsy/grunt-regex-replace/blob/master/package.json
The argument to the replace function use case are:
argument[0] is the full string matched
argument[1] is the first parenthesized substring match
argument[2] is the second parenthesized substring match
argument[3] is the Nth parenthesized substring match ...
see https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions and the section "Using parenthesized substring matches"
Currently the task only accepts string arguments.
This is ok for simple expressions but for larger ones the escaping gets tedious:
search: '}\\);\\s*(\\/\\/@\\s*sourceMappingURL=[\\w-\\.]+.js.map\\s*)?$'
Since grunt is javascript anyway it might be cool to support passing-in RegExp objects as search
.
Also it could be good to allow replace
to be a function just like original String.replace
. You could add additional arguments with the name of the action and it's value for extra dynamics.
At the moment this plugin generates a lot of logging, two lines for each file. This generates many pages of logging which obscures our more important stuff. Is it possible to suppress this?
If not, is this an interesting feature to add?
This is my first attempt to use a grunt plugin, so please forgive me if I am missing something obvious -- but I think I'm following the directions from Grunt and from you, and running into an error. My package.json looks like this:
{
"name": "brink-prototype",
"version": "0.0.0",
"dependencies": {
"bower": "~0.8.6",
"grunt": "~0.4.1",
"grunt-regex-replace": "~0.2.2"
}
}
And my Gruntfile.js looks like this:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
'regex-replace': {
src: ['components/bootstrap/less/navbar.less'],
actions: [
{
search: '\.box-shadow(.*);$',
replace: '',
flags: ''
}
]
}
});
grunt.loadNpmTasks('grunt-regex-replace');
grunt.registerTask('default', ['regex-replace']);
};
Now, when I run grunt from the command line I get the following error:
Running "regex-replace:src" (regex-replace) task
Warning: Cannot read property 'src' of undefined Use --force to continue.
Aborted due to warnings.
Do you have any idea what I'm doing wrong? Thanks so much for your help.
I'm posting this issue to let you know that we will be publishing Grunt 0.4 on Monday, February 18th.
If your plugin is not already Grunt 0.4 compatible, would you please consider updating it? For an overview of what's changed, please see our migration guide.
If you'd like to develop against the final version of Grunt before Monday, please specify "grunt": "0.4.0rc8"
as a devDependency in your project. After Monday's release, you'll be able to use "grunt": "~0.4.0"
to actually publish your plugin. If you depend on any plugins from the grunt-contrib series, please see our list of release candidates for compatible versions. All of these will be updated to final status when Grunt 0.4 is published.
Also, in an effort to reduce duplication of effort and fragmentation in the developer community, could you review the grunt-contrib series of plugins to see if any of your functionality overlaps significantly with them? Grunt-contrib is community maintained with 40+ contributors—we'd love to discuss any additions you'd like to make.
Finally, we're working on a new task format that doesn't depend on Grunt: it's called node-task. Once this is complete, there will be one more conversion, and then we'll never ask you to upgrade your plugins to support our changes again. Until that happens, thanks for bearing with us!
If you have any questions about how to proceed, please respond here, or join us in #grunt on irc.freenode.net.
Thanks, we really appreciate your work!
There is a small typo in README.md
:
Example af an object.
Hello:
My example:the test to be replaced
#version 2013-10-14
The following works:
replaceManifestVer: {
src: ['<%= pkg.targetFolder %>/<%= pkg.help_release %>/help.manifest'],
actions: [
{
name: 'update manifest ver',
search: '#version.+\-',
replace: '',
flags: 'g'
}
]
}
The txt will leave '14', but the following doesn't work.
{
name: 'update manifest ver',
search: '#version\s+\-',
replace: '',
flags: 'g'
}
So \s can't be recognized. Why?
Support for this.data.actions to be a function that returns an array, as well as a raw array.
https://github.com/bomsy/grunt-regex-replace/blob/master/tasks/regex-replace.js#L19
Allowing:
// This will ensure the first 25 LINK element have closing /> form
"regex-replace": {
fixXhtmlLinkElements: {
src: [ '**/*.html' ],
actions: function() {
var func = function(s) {
if(arguments.length > 2) {
var l = arguments[1].length; // length of match0
var ch = '';
if(l > 0)
ch = arguments[1].charAt(l - 1); // get last char
var insert = (ch === '/') ? '' : '/'; // decide if we need to insert '/' or not
return arguments[1] + insert + arguments[2];
} else {
return arguments[0];
}
};
var ary = [];
for(var i = 1; i <= 25; i++) {
var o = {
name: 'link' + i,
search: new RegExp('(<link\\s+[^>]*)(>)'),
replace: func,
flags: 'gi'
};
ary.push(o);
}
return ary;
}
}
},
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.