对字符串进行HTML编码和解码的JavaScript函数
对字符串进行HTML编码和解码的JS函数,记下来,方便找 :)
编码函数:
function htmlEncode(str) { var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/\'/g, "'"); s = s.replace(/\"/g, """); return s; }
解码函数:
function htmlDecode(str){ var s = ""; if (str.length == 0) return ""; s = str.replace(/&/g, "&"); s = s.replace(/</g, "<"); s = s.replace(/>/g, ">"); s = s.replace(/'/g, "\'"); s = s.replace(/"/g, "\""); return s; }