提交表单中的特殊字符转换

<script type="text/javascript">
// 替换特殊字符
function HTMLEncode(text){
 text = text.replace(/&/g, "&amp;") ;
 text = text.replace(/"/g, "&quot;") ;
 text = text.replace(/</g, "&lt;") ;
 text = text.replace(/>/g, "&gt;") ;
 text = text.replace(/'/g, "&#146;") ;
 text = text.replace(/\ /g,"&nbsp;");
 text = text.replace(/\n/g,"<br>");
 text = text.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
 return text;
}
//还原特殊字符
function TEXTAREAcode(text){
 text = text.replace(/\n/g,"");
 text = text.replace(/&amp;/g, "&") ;
 text = text.replace(/&quot;/g, "\"") ;
 text = text.replace(/&lt;/g, "<") ;
 text = text.replace(/&gt;/g, ">") ;
 text = text.replace(/&#146;/g, "\'") ;
 text = text.replace(/&nbsp;/g," ");
 text = text.replace(/<br>/g,"\n");
 return text;
}

posted @ 2014-06-27 13:48  yangfeizbj  阅读(530)  评论(0编辑  收藏  举报