4 回答

TA貢獻1825條經驗 獲得超4個贊
自己寫了這樣的代碼,目前是能解決已經想到的代碼匹配了,肯定還有坑
str.replace(/[\x20\S]*\/\/[\x20\S]*?\n?$/gm, function(v) {
if (/^\x20*\/\/|^\x20*\/\//.test(v)) { return '' }
if (/[,;]\x20*\/\//.test(v)) { return v.replace(/([,;])\x20*\/\/[\x20\S]*?\n?$/m, '$1') }
return v
})
后來又發現了這樣的代碼:
{
a:"aa",
b:"bb"//目標字段名
}
wtf

TA貢獻1883條經驗 獲得超3個贊

TA貢獻1789條經驗 獲得超8個贊
做了很多嘗試,看看這個。
removeComment(`' : //'+host;// 123456`); // ' : //'+host;
function removeComment(str) {
const reg = /("([^\\\"]*(\\.)?)*")|('([^\\\']*(\\.)?)*')|(\/{2,}.*?(\r|\n|$))|(\/\*(\n|.)*?\*\/)/g;
return str.replace(reg, function(s) {
return /^\/{2,}/.test(s) || /^\/\*/.test(s) ? "" : s;
});
}
添加回答
舉報