首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

javascript实施eval函数时利用正则表达式去掉回车符换行符和注释

2012-11-09 
javascript执行eval函数时利用正则表达式去掉回车符换行符和注释var txtValue document.getElementById(

javascript执行eval函数时利用正则表达式去掉回车符换行符和注释
var txtValue = document.getElementById(txtId).value;
txtValue = txtValue.replace(/\/\*((\n|\r|.)*?)\*\//mg, "");//去掉多行注释/*..*/
txtValue = txtValue.replace(/(\s+)\/\/(.*)\n/g,"");//去掉单行注释//(前面有空格的注释)
txtValue = txtValue.replace(/;\/\/(.*)\n/g,";");//去掉单行注释//(前面是分号的注释)
txtValue = txtValue.replace(/\/\/[^"][^']\n/g,"");//去掉单行注释//(//后面只有一个'或一个"的不替换)
txtValue = txtValue.replace(/[\r]/g,"");//替换换行
txtValue = txtValue.replace(/[\n]/g,"");//替换回车
eval(txtValue);

热点排行