String.replace

function convertStr(inputText){
var tempStr = inputText;
var RexStr = /\<|\>|\"|\'|\&/g
    tempStr = tempStr.replace(RexStr,
        function(MatchStr){
            switch(MatchStr){
                case "<":
                    return "&lt;";
                    break;
                case ">":
                    return "&gt;";
                    break;
                case "\"":
                    return "&quot;";
                    break;
                 case "'":
                    return "&#39;";
                    break;
                case "&":
                    return "&amp;";
                    break;
                default :
                    break;
            }
        }
    )
   return tempStr;
}

 

你可能感兴趣的:(replace)