日期:2014-05-20  浏览次数:20691 次

撼动正则表达式天地?!
谁能给出下面正确的解呢
如下的代码,如何用正则表达式正确的去除注释:
var   kk   =   "//   sljdfljsdf ";   //   *skldflsfllk "sdlf//sdfdsf "
var   kk   =   "//   sljdfljsdf ";   //   *skldflsfllk "sdlf//sdfdsf "
var   kk   =   "//   sljdfljsdf ";   //   *skldflsfllk "sdlf//sdfdsf "
var   kk   =   "//   sljdf/*sdf*/ljsdf ';   //   *sk/*fdsf*/ld " "flsfllk "sdlf//sdfdsf "
var   kk   =   '//   sljdf/*sdf*/ljsdf ';   //   *sk/*fdsf*/ld " "flsfllk "sdlf//sdfdsf "
var   kk   =   '//   sljdf/*sdf*/ljsdf ';   //   *sk/*fdsf*/ld " "flsfllk "sdlf//sdfdsf "

------解决方案--------------------
mark
------解决方案--------------------
不明白!// 后面的不都是注释吗?
------解决方案--------------------
关键是 "的处理,呵呵
------解决方案--------------------
kk.replace(/^\/\/\\*w+$/, ' ');
不知道行不!!!
------解决方案--------------------
正则表达式如下:
//(? <=;\s*//)[^\n]+
------解决方案--------------------
楼上的就可以,以分号为分界点.
------解决方案--------------------
js不支持反向预搜索
------解决方案--------------------
不懂,顶一下.
------解决方案--------------------
我想问一下,弄这个有什么用吗?
------解决方案--------------------
感觉是很闲的问题
------解决方案--------------------
不会
------解决方案--------------------
呃....我什么都不懂...
------解决方案--------------------
<span id= "text " style= "display:none ">
<pre>
var kk = "// sljdfljsdf "; // *skldflsfllk "sdlf//sdfdsf "
var kk = "// sljdfljsdf "; // *skldflsfllk "sdlf//sdfdsf "
var kk = "// sljdfljsdf "; // *skldflsfllk "sdlf//sdfdsf "
var kk = "// sljdf/*sdf*/ljsdf ' // *sk/*fdsf*/ld " "flsfllk "sdlf//sdfdsf "
var kk = '// sljdf/*sdf*/ljsdf '; // *sk/*fdsf*/ld " "flsfllk "sdlf//sdfdsf "
var kk = '// sljdf/*sdf*/ljsdf ' // *sk/*fdsf*/ld " "flsfllk "sdlf//sdfdsf "
var kk = new Object(); //test
</pre>
</span>

<script>
var reg = /([^ ' "\/\n]*([ ' "]?).*?[^\\]\2(?:\s*;\s*|\s+))(?:\/\/.*|\/\*.*\*\/\s*)(\n|$)/g;
var kk = document.getElementById( "text ").innerText;
alert( "处理前:\n " + kk);
alert( "处理后:\n " + kk.replace(reg, "$1$3 "));
</script>
------解决方案--------------------
.........................................
------解决方案--------------------
程序员群 41801837
------解决方案--------------------
http://community.csdn.net/Expert/topic/5541/5541735.xml?temp=.7890283