How to decode HTML entities using jQuery?

后端 未结 19 2092
忘了有多久
忘了有多久 2020-11-21 23:21

How do I use jQuery to decode HTML entities in a string?

相关标签:
19条回答
  • 2020-11-22 00:03

    Suppose you have below String.

    Our Deluxe cabins are warm, cozy & comfortable

    var str = $("p").text(); // get the text from <p> tag
    $('p').html(str).text();  // Now,decode html entities in your variable i.e 
    

    str and assign back to

    tag.

    that's it.

    0 讨论(0)
  • 2020-11-22 00:03

    To decode HTML Entities with jQuery, just use this function:

    function html_entity_decode(txt){
        var randomID = Math.floor((Math.random()*100000)+1);
        $('body').append('<div id="random'+randomID+'"></div>');
        $('#random'+randomID).html(txt);
        var entity_decoded = $('#random'+randomID).html();
        $('#random'+randomID).remove();
        return entity_decoded;
    }
    

    How to use:

    Javascript:

    var txtEncoded = "&aacute; &eacute; &iacute; &oacute; &uacute;";
    $('#some-id').val(html_entity_decode(txtEncoded));
    

    HTML:

    <input id="some-id" type="text" />
    
    0 讨论(0)
  • 2020-11-22 00:04

    You can use the he library, available from https://github.com/mathiasbynens/he

    Example:

    console.log(he.decode("J&#246;rg &amp J&#xFC;rgen rocked to &amp; fro "));
    // Logs "Jörg & Jürgen rocked to & fro"
    

    I challenged the library's author on the question of whether there was any reason to use this library in clientside code in favour of the <textarea> hack provided in other answers here and elsewhere. He provided a few possible justifications:

    • If you're using node.js serverside, using a library for HTML encoding/decoding gives you a single solution that works both clientside and serverside.

    • Some browsers' entity decoding algorithms have bugs or are missing support for some named character references. For example, Internet Explorer will both decode and render non-breaking spaces (&nbsp;) correctly but report them as ordinary spaces instead of non-breaking ones via a DOM element's innerText property, breaking the <textarea> hack (albeit only in a minor way). Additionally, IE 8 and 9 simply don't support any of the new named character references added in HTML 5. The author of he also hosts a test of named character reference support at http://mathias.html5.org/tests/html/named-character-references/. In IE 8, it reports over one thousand errors.

      If you want to be insulated from browser bugs related to entity decoding and/or be able to handle the full range of named character references, you can't get away with the <textarea> hack; you'll need a library like he.

    • He just darn well feels like doing things this way is less hacky.

    0 讨论(0)
  • 2020-11-22 00:05

    Alternatively, theres also a library for it..

    here, https://cdnjs.com/libraries/he

    npm install he                 //using node.js
    
    <script src="js/he.js"></script>  //or from your javascript directory
    

    The usage is as follows...

    //to encode text 
    he.encode('© Ande & Nonso® Company LImited 2018');  
    
    //to decode the 
    he.decode('&copy; Ande &amp; Nonso&reg; Company Limited 2018');
    

    cheers.

    0 讨论(0)
  • 2020-11-22 00:07

    Use

    myString = myString.replace( /\&amp;/g, '&' );
    

    It is easiest to do it on the server side because apparently JavaScript has no native library for handling entities, nor did I find any near the top of search results for the various frameworks that extend JavaScript.

    Search for "JavaScript HTML entities", and you might find a few libraries for just that purpose, but they'll probably all be built around the above logic - replace, entity by entity.

    0 讨论(0)
  • 2020-11-22 00:07

    Try this :

    var htmlEntities = "&lt;script&gt;alert('hello');&lt;/script&gt;";
    var htmlDecode =$.parseHTML(htmlEntities)[0]['wholeText'];
    console.log(htmlDecode);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    parseHTML is a Function in Jquery library and it will return an array that includes some details about the given String..

    in some cases the String is being big, so the function will separate the content to many indexes..

    and to get all the indexes data you should go to any index, then access to the index called "wholeText".

    I chose index 0 because it's will work in all cases (small String or big string).

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