<script language=javascript>
function coder(str)
{
   var s = "";
   if (str.length == 0) return "";
   for (var i=0; i<str.length; i++)
   {
      switch (str.substr(i,1))
      {
          case "<"  : s += "&lt;";   break;
          case ">"  : s += "&gt;";   break;
          case "&"  : s += "&amp;";  break;
          case " "  : s += "&nbsp;"; break;
          case "\'" : s += "&#39;";  break;
          case "\"" : s += "&quot;"; break;
          case "\n" : s += "<br>";   break;
          default   : s += str.substr(i,1); break;
      }
   }
   return s;
}
</script>

解决方案 »

  1.   

    在javascript中有一个替换函数跟vb一样src.replaceAll("","");  但是使用二楼的"哥哥" 的方法是最好的了!  ^_^
      

  2.   

    javascript本身就是有replace这个函数的....
    具体用法如下:replace 方法
      返回根据正则表达式进行文字替换后的字符串的复制。stringObj.replace(rgExp, replaceText)参数
    stringObj 必选项。要执行该替换的 String 对象或字符串文字。该字符串不会被 replace 方法修改。 rgExp 必选项。为包含正则表达式模式或可用标志的正则表达式对象。也可以是 String 对象或文字。如果 rgExp 不是正则表达式对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。replaceText 必选项。是一个String 对象或字符串文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中,replaceText 参数也可以是返回替换文本的函数。下面的示例演示了 replace 方法将第一次出现的单词 "The" 替换为单词 "A" 的用法。function ReplaceDemo(){
       var r, re;                    // 声明变量。
       var ss = "The man hit the ball with the bat.\n";
       ss += "while the fielder caught the ball with the glove.";
       re = /The/g;             // 创建正则表达式模式。
       r = ss.replace(re, "A");    // 用 "A" 替换 "The"。
       return(r);                   // 返回替换后的字符串。
    }
    另外, replace 方法也可以替换模式中的子表达式。 下面的范例演示了交换字符串中的每一对单词: function ReplaceDemo(){
       var r, re;                      // 声明变量。
       var ss = "The rain in Spain falls mainly in the plain.";
       re = /(\S+)(\s+)(\S+)/g;        // 创建正则表达式模式。
       r = ss.replace(re, "$3$2$1");   // 交换每一对单词。
       return(r);                      // 返回结果字符串。
    }
    下面的示例(在 JScript 5.5 及更新版本中执行)执行的是从华氏到摄氏的转换,它演示了使用函数作为 replaceText。要想知道该函数是如何工作的,传递一个包含数值的字符串,数值后要紧跟 "F" (例如 "Water boils at 212")。function f2c(s) {
      var test = /(\d+(\.\d*)?)F\b/g;    // 初始化模式。
      return(s.replace
        (test,
          function($0,$1,$2) { 
            return((($1-32) * 5/9) + "C");
          }
        )
      );
    }
    document.write(f2c("Water freezes at 32F and boils at 212F."));
      

  3.   

    严重更正 src.replaceAll("","");---->  src.replace("","");呵呵 不好意思 上面的写错了  和 java搞混了!
      

  4.   

    如果字符串var str="AA&BB<CC>DD'EE\"FF\tGG\nHH\\nII\\tJJ";
    如何生成XML啊? var xmlString="<?xml version=\"1.0\" encoding=\"gb2312\" ?>";
    xmlString+="<SendInfo>"
    xmlString+=RStringXML(window.txtContents.value);
    xmlString+="</SendInfo>"
    window.alert(xmlString);
    //return;

    var xmlDocument = new ActiveXObject("Msxml2.DOMDocument");
    xmlDocument.async = false;
    xmlDocument.loadXML(xmlString);
    window.alert(xmlDocument.xml);//上面这么作总是失败 :(help...
      

  5.   

    搞定了 我把&apos;写成&pos;了结帐
      

  6.   

    var str = "a1b2c3d45e6f7";
    alert(str.replace(/\D/g, ''));