0

I know how to search and replace strings with Sublime Text2 but not when escape characters are used.

For the following javascript code can someone tell me how to escape the parentheses here?

if(PRODUCTION) console.log("hello world");

var log=function(message){
   if(!PRODUCTION) console.log(message);
};

The following does not work but seems close to being right:

find: if(PRODUCTION) console.log("(\w+)");

replace: log($1);

The ultimate goal is to only do console logging if in a development and not a production environment.

Community
  • 1
  • 1
tim peterson
  • 23,653
  • 59
  • 177
  • 299

1 Answers1

2

Escape special characters with backslash:

Find: if\(PRODUCTION\) console.log\((".+?")\);

Replace: log($1)

Note that \w+ won't match hello world, because \w doesn't match space. I've changed it to .+? so it will match anything until the matching doublequote.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    I know this bit is in the OP's code, but a little improvement could be removing the quotes so any call to console log gets replaced: `console.log\((.+?)\)` (i.e. console.log(someVar); ) – Juan Pablo Califano May 08 '14 at 20:42