前言
近期项目中遇到需求,需要处理掉从Github上获取到的JavaScript代码中的注释,因为正则方法很难完全去除各种形式的注释。故找到了一个叫StrollingWolf的工具,号称能够去除多种语言中的各种形式的注释。试用之后发现确实效果还不错,我以为它的原理是基于语法树去除的,然而在正式在项目中使用它时却发生了意外。
重现
首先发现其处理过后的数据中有莫名其妙的缺失,于是慢慢排查,发现确实是由于这个工具引起的。例子:
function(input) {
input = input.replace(/'/g, "''")
// backslashes (\) must be replaced with double backslashes (\\)
input = input.replace(/\\/g, '\\\\')
// double quotes (") must be replaced with escaped quotes (\\")
input = input.replace(/"/g, '\\"')
return input
}
function() {
this.title = "OpenClipart.org";
this.name = "OpenClipart.org";
this.uri = "https://openclipart.org/";
this.icon = "https://openclipart.org/favicon.ico";
this.baseUri = "https://openclipart.org/search/json/&