HtmlSpecialChars equivalent in Javascript?

后端 未结 16 1518
耶瑟儿~
耶瑟儿~ 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:15
    function htmlEscape(str){
        return str.replace(/[&<>'"]/g,x=>'&#'+x.charCodeAt(0)+';')
    }
    

    This solution uses the numerical code of the characters, for example < is replaced by &#60;.

    Although its performance is slightly worse than the solution using a map, it has the advantages:

    • Not dependent on a library or DOM
    • Pretty easy to remember (you don't need to memorize the 5 HTML escape characters)
    • Little code
    • Reasonably fast (it's still faster than 5 chained replace)
    0 讨论(0)
  • 2020-11-22 06:17

    There is a problem with your solution code--it will only escape the first occurrence of each special character. For example:

    escapeHtml('Kip\'s <b>evil</b> "test" code\'s here');
    Actual:   Kip&#039;s &lt;b&gt;evil</b> &quot;test" code's here
    Expected: Kip&#039;s &lt;b&gt;evil&lt;/b&gt; &quot;test&quot; code&#039;s here
    

    Here is code that works properly:

    function escapeHtml(text) {
      return text
          .replace(/&/g, "&amp;")
          .replace(/</g, "&lt;")
          .replace(/>/g, "&gt;")
          .replace(/"/g, "&quot;")
          .replace(/'/g, "&#039;");
    }
    

    Update

    The following code will produce identical results to the above, but it performs better, particularly on large blocks of text (thanks jbo5112).

    function escapeHtml(text) {
      var map = {
        '&': '&amp;',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        "'": '&#039;'
      };
      
      return text.replace(/[&<>"']/g, function(m) { return map[m]; });
    }
    
    0 讨论(0)
  • 2020-11-22 06:19

    For Node.JS users (or users utilizing Jade runtime in the browser), you can use Jade's escape function.

    require('jade').runtime.escape(...);
    

    No sense in writing it yourself if someone else is maintaining it. :)

    0 讨论(0)
  • 2020-11-22 06:21

    Yet another take at this is to forgo all the character mapping altogether and to instead convert all unwanted characters into their respective numeric character references, e.g.:

    function escapeHtml(raw) {
        return raw.replace(/[&<>"']/g, function onReplace(match) {
            return '&#' + match.charCodeAt(0) + ';';
        });
    }
    

    Note that the specified RegEx only handles the specific characters that the OP wanted to escape but, depending on the context that the escaped HTML is going to be used, these characters may not be sufficient. Ryan Grove’s article There's more to HTML escaping than &, <, >, and " is a good read on the topic. And depending on your context, the following RegEx may very well be needed in order to avoid XSS injection:

    var regex = /[&<>"'` !@$%()=+{}[\]]/g
    
    0 讨论(0)
  • 2020-11-22 06:22
    String.prototype.escapeHTML = function() {
            return this.replace(/&/g, "&amp;")
                       .replace(/</g, "&lt;")
                       .replace(/>/g, "&gt;")
                       .replace(/"/g, "&quot;")
                       .replace(/'/g, "&#039;");
        }
    

    sample :

    var toto = "test<br>";
    alert(toto.escapeHTML());
    
    0 讨论(0)
  • 2020-11-22 06:22

    I am elaborating a bit on o.k.w.'s answer.

    You can use the browser's DOM functions for that.

    var utils = {
        dummy: document.createElement('div'),
        escapeHTML: function(s) {
            this.dummy.textContent = s
            return this.dummy.innerHTML
        }
    }
    
    utils.escapeHTML('<escapeThis>&')
    

    This returns &lt;escapeThis&gt;&amp;

    It uses the standard function createElement to create an invisible element, then uses the function textContent to set any string as its content and then innerHTML to get the content in its HTML representation.

    0 讨论(0)
提交回复
热议问题