Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top