HtmlSpecialChars equivalent in Javascript?

后端 未结 16 1508
耶瑟儿~
耶瑟儿~ 2020-11-22 06:00

Apparently, this is harder to find than I thought it would be. And it even is so simple...

Is there a function equivalent to PHP\'s htmlspecialchars built into Javas

16条回答
  •  囚心锁ツ
    2020-11-22 06:22

    String.prototype.escapeHTML = function() {
            return this.replace(/&/g, "&")
                       .replace(//g, ">")
                       .replace(/"/g, """)
                       .replace(/'/g, "'");
        }
    

    sample :

    var toto = "test
    "; alert(toto.escapeHTML());

提交回复
热议问题