replace方法

 1 function htmlEscape(text) {
 2             return text.replace(/[<>"&]/g, function (match, pos, originalText) {
 3                 switch (match) {
 4                     case "<":
 5                         return "&lt;";
 6                     case ">":
 7                         return "&gt;";
 8                     case "&":
 9                         return "&amp;";
10                     case "\"":
11                         return "&quot;";
12                 }
13             });
14         }
15         console.log(htmlEscape("<p class=\"greeting\">Hello world!</p>"));
posted @ 2012-03-15 14:13  小猩猩君  阅读(220)  评论(0编辑  收藏  举报