Click here to Skip to main content
15,437,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
(Regex Javascript older Node.js version)

I need to parse to remove empty if else and for blocks without actually using parser.

input str1="   if(x>100){  }  

if  (x<20)

}  else    if(x>20){      

y=changes_values; or maybe return y; let's just say this is not empty

else {   }"

output str2="
if  (x<20)

}  else   if(x>20){     

y=changes_values; or maybe return y; let's just say this is not empty

What I have tried:

so far I have come up with this but I need help

<pre>const regex3= /(?<=(if\s*)|(else\s*) | (else\s*)(if\s*)) \(.*\))\s*\{(([^}])*)\}/gim
Updated 11-Feb-22 9:03am

1 solution

Regexes are wonderful things, and a very powerful tool.

But ... they are text processors, not syntactic analysers, not parsers, not tokenizers, not any stage of a compiler.

While it's probably possible to do what you want with one, it's a poor idea because the regex is going to get really complicated, really quickly - and still won't work well under all circumstances. For example, what happens with comments? they can contain anything - and be anywhere - so your regex will have to cope with them as well. And that means when the else is inside a comment, or a comment is inside the else.
And what happens when someone writes
if ...
else if ...
Remove the "empty else" and you change the program logic significantly.
I'm sure there are probably a dozen more "special cases" that your regex may have to cope with, and that's when it gets really nasty and you spend more time fighting bugs in the regex than you do coding it ...

Use a tokenizer, use a parser. Regex is a good hammer, but it's not the only tool in the box, so don't treat every problem as a nail!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900