开发者

C# Regular Expression to comment out console.log

开发者 https://www.devze.com 2023-04-03 17:06 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消