读书人

javascript实施eval函数时利用正则表达

发布时间: 2012-11-09 10:18:47 作者: rapoo

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);

读书人网 >JavaScript

热点推荐