C# Regular Expression to comment out console.log
I have build a small deployment .exe in c# to do a few things like combine and min my JS and CSS.
One thing I need is a regular expression that I can use to co开发者_运维百科mment out all console.log statements in my JavaScript files.
This can get you started:
var str = "hello console.log('hello'); bye";
var replaced = Regex.Replace(str, @"(?<log>console\.log\(.*?\);?)", @"/* ${log} */");
Basically what we are doing is looking for instance of console.log(...)
with a non-greedy capture between the parenthesis, and optionally capture the semicolon after console.log
.
Note that if your call to console.log
's content includes parens, like console.log('Hello (bye)');
, then this doesn't handle that - but you should be able to tweak it to get that much working.
精彩评论