HtmlSpecialChars equivalent in Javascript?

后端 未结 16 1556
耶瑟儿~
耶瑟儿~ 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:31

    function htmlspecialchars(str) {
     if (typeof(str) == "string") {
      str = str.replace(/&/g, "&"); /* must do & first */
      str = str.replace(/"/g, """);
      str = str.replace(/'/g, "'");
      str = str.replace(//g, ">");
      }
     return str;
     }
    

提交回复
热议问题