zoukankan      html  css  js  c++  java
  • javascript 替换有害字符(学习一下prototype与javascript正则)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        
    <title>javascript 替换有害字符(学习一下prototype与javascript正则)</title>
        
    <script type="text/javascript">
            
    /***********************************
             /g是global全局替换,如果没有这个标识,只替换第一处
             /asdf/gi,i是忽略大小写。
             还有个m不常用,是否换行匹配
            **********************************
    */
            String.prototype.htmlEncode 
    = function(){
                
    return this.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\n/g,"<br />");
            };
            
    function f(){
                document.getElementById(
    "div1").innerHTML="<00<0>".htmlEncode();//编码之后
                alert("<00<0>".htmlEncode());
                document.getElementById(
    "div2").innerHTML=document.getElementById("Text1").value.htmlEncode();//输入<>/n之类试试
                alert(document.getElementById("Text1").value.htmlEncode());
            }
        
    </script>
    </head>
    <body>
    <input id="Text1" type="text" value="<>\n" />
    <div id="div1">hello</div>
    <input type="button" value="ok" onclick="f();" />
    <div id="div2">hi</div>
    </body>
    </html>
  • 相关阅读:
    Opencv中cvCopy()和cvCloneImage()的区别(转)
    什么是DC?
    UpdateDate()函数的作用
    宏定义函数指针类型【转】
    将openCV中的IplImage格式的图片显示到Picture控件上
    C程序调用shell脚本共有三种方法
    memset ,memcpy ,memmove,strcpy 的根本区别 与实现
    sizeof strlen length()
    itoa 实现
    string 与 char * 转换
  • 原文地址:https://www.cnblogs.com/greatverve/p/1534274.html
Copyright © 2011-2022 走看看