s4m0k
s4m0k

Reputation: 568

RegEx : Match line of string that does not contain [ character

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*

Demo Here

But it selects everything from .add string.

Can anyone point me to the the right direction? Thanks in advance!

Upvotes: 3

Views: 273

Answers (2)

anubhava
anubhava

Reputation: 785531

This regex should work:

\.add *\([^,]+, +[^[][^{]*{$

with g and m flags.

Working Demo

Upvotes: 2

kapa
kapa

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

Related Questions