Skip to content

Instantly share code, notes, and snippets.

@ggamir
Created October 15, 2015 23:09
Show Gist options
  • Save ggamir/e6bf4aa3cc54a3d05046 to your computer and use it in GitHub Desktop.
Save ggamir/e6bf4aa3cc54a3d05046 to your computer and use it in GitHub Desktop.
regex for if(a) b; -----> if(a) { b;}
/*
Hidden in the deep, dark recesses of code
there lives an evil kind of if-statement.
Small, fragile and incomplete.
It has a weakness that weakens the whole.
Should anyone aside from the creator so much as graze it,
a living infection will spread, breaking everything.
We must not let these pests run wylde.
Deep in the mountains of a text editor,
we have forged a weapon to combat them:
One Regex to rule them all, One Regex to find them,
One Regex to bring them all and in the darkness bind them:
SEARCH: /(^\s*if\s?\(.*\))\s*([^{].*;)\s*$/
REPLACE: \1 { \2 }
This is a regex for finding single-line if-statements without curly braces, and adding curlies in.
eg. if(true) return; --> if(true) { return; }
It makes some assumptions, and so it might not work with complex situations:
eg. nested parenthesis, or existing curlies from an anonymous function.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment