Reputation: 568
Another RegEx question that I could not solve myself:
I have this JavaScript file with the following content:
var myApp = new App();
function ExplicitFunction ( one, two, three ) {
}
/** This line should match from .add ("ModuleA", ExplicitFunction ) { */
myApp.add ("ModuleA", ExplicitFunction ) {
}
/** This line should not match at all because of [ character after "ModuleB", */
myApp.add ("ModuleB", ["p1", "p2", function (p1, p2) {
}]);
/** This line should match from .add ("ModuleC", function(paramA, paramB) { */
myApp.add ("ModuleC", function(paramA, paramB) {
});
Based from the codes above, I want to write a RegEx that will find from this file those codes that are not following this coding convention; Please note of the array parameter.
.add ("Some String Here", [ anything here string or function ]);
So Basically, As what the comments /** */ on the file is describing
myApp.add ("ModuleB", ["p1", "p2", function (p1, p2) {
}]);
should not match because it follows the coding convention describe above.
and the other lines that is calling myApp.add should match because it does not follow the convention.
I have tried the following :
\.add\s*\({1}.*[^\[]\w*
But it selects everything from .add string.
Can anyone point me to the the right direction? Thanks in advance!
Upvotes: 3
Views: 273
Reputation: 785531
This regex should work:
\.add *\([^,]+, +[^[][^{]*{$
with g
and m
flags.
Upvotes: 2
Reputation: 78731
Something like this could work:
\.add\s*\([^\[\n]*{$
.add
, then possible whitespace, then (
, then stuff that is not [
or a line-break, and finally a {
closing the line.
All this with a \m
switch that makes $
match end of the line.
It could of course be improved and tailored to your needs, but might be a good start.
Upvotes: 1