0

How to convert multibyte characters like curly quotes to their equivalent entity like “ using jquery or javascript?

var userTxt = '“testing”';  

after converting userTxt should look like => “testing”

Satish Gadhave
  • 2,880
  • 3
  • 20
  • 27
  • 1
    Shouldn't you use `var userTxt = '“testing”';` – Optimus Prime Jul 31 '13 at 13:26
  • Out of interest, why would you want to do that? If you’re having display problems with characters like this, setting your `Content-Type` HTTP header to `text/html; charset=utf-8` (or an appropriate value if you’re using a different encoding than utf-8) should sort them out. – Paul D. Waite Jul 31 '13 at 13:32
  • 1
    http://stackoverflow.com/a/784765/2220391 works http://jsfiddle.net/Spokey/8ftfS/ – Spokey Jul 31 '13 at 13:35

3 Answers3

0

Here's how to do it:

$('<div/>').text('This is fun & stuff').html(); // evaluates to "This is fun &amp; stuff"

Source

Or you can do it this way.

Kamil Szymański
  • 950
  • 12
  • 26
0

try instead if you can use $quot instead of &#8220

var e_encoded = e.html().replace(/"/g, "&quot;");
console.log(e_encoded); // outputs &quot;&amp;

or you can use this function

function htmlEscape(str) {
    return String(str)
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
}
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

You can do this using regular expressions.

function replace_quotes( text ){
    return text.replace(/\u201C/g, "&#8220;").replace(/\u201D/g, "&#8221;");
}

This function replaces the quote characters by matching their unicode hex code.
See: Regex Tutorial - Unicode Characters

Splendiferous
  • 733
  • 2
  • 6
  • 17