我写了这个:
grep -lr --include=*.js "eval(function" sitemap.js | xargs sed -i.bak 's/}eval(function[^;]*;/{\n/g'
这是我要删除的代码:
}eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('7 3(e,t,n,a,o,i,m){e.5=o,e[o]=e[o]||3(){(e[o].q=e[o].q||[]).b(p)},e[o].l=e[o].l||+6 4,i=t.8(n),i.9=1,i.c=a,m=t.d(n)[0],m.f.g(i,m)}(h,j,"k","//2.r.s/2/1.u","2");',31,31,'||tj|function|Date|alogObjectName|new|void|createElement|asyn||push|src|getElementsByTagName||parentNode|insertBefore|window||document|script|||||arguments||tongjj|info||js'.split('|'),0,{}))
我的结果是这样的:(只删除了一些。第一位)
if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('7 3(e,t,n,a,o,i,m){e.5=o,e[o]=e[o]||3(){(e[o].q=e[o].q||[]).b(p)},e[o].l=e[o].l||+6 4,i=t.8(n),i.9=1,i.c=a,m=t.d(n)[0],m.f.g(i,m)}(h,j,"k","//2.r.s/2/1.u","2");',31,31,'||tj|function|Date|alogObjectName|new|void|createElement|asyn||push|src|getElementsByTagName||parentNode|insertBefore|window||document|script|||||arguments||tongjj|info||js'.split('|'),0,{}))
是什么导致我的正则表达式损坏以及如何修复它?
目标: 我想删除整个 EVAL() 字符串/行
最佳答案
eval() 调用在该行的最后一个括号处结束。 sed 无法匹配方括号和圆括号。您可能需要为此编写一个程序。
否则,请在括号匹配编辑器中打开代码并手动将其删除。
关于regex - grep 和替换只能工作一半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33418599/