2

JS FIDDLE

I have a function as follows in javascript

function myfun()
{
  var a = "'\u0c39";
  alert(a);

}

When i called this function i am able to see correct letter(This letter is telugu language letter).

My problem is my string is coming from java which is like "'\\u0c39". I used as follows

function myfun()
{
  var a = "'\\u0c39";
  alert(a.replace("\\\\","\\").toString());

}

But in alert '\u0c39 is coming.What might be the mistake here.Please help me.Thanks in advance...

PSR
  • 39,804
  • 41
  • 111
  • 151

2 Answers2

5

Working in IE 10 (including back to IE7 mode): http://jsfiddle.net/FersM/2/

Try this:

function myfun()
{
  var a = "'\\u0c39";
  a = a.replace(/\\u([a-f0-9]{4})/gi, function (n, hex) {
      return String.fromCharCode(parseInt(hex, 16));
  });

  alert(a);

}

If you want to understand it, the regular expression is looking for all instances of a backslash (double-escaping needed within a regex literal) followed by exactly 4 hexadecimal digits (of whatever case), and then the function will replace the contents of each sequence thus found with the return value. The return value uses fromCharCode to convert a Unicode "code point" value into the actual character, but it requires the code point as a number (decimal), so we must first convert the 4 hex characters (the first parenthetical match, and thus the 2nd argument of the function, with the first argument being the whole sequence match which we don't need) into a decimal using the parseInt function with the base argument as "16".

Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
1

You can use this eval code for that issue

http://jsfiddle.net/6Hmyk/

function myfun1()
{
  var a = "'\\u0c39";

   alert(eval('"'+a+'"'));   

}

myfun1();

In that code , \\ will change to \ and eval code run this as unicode character

Taleh Ibrahimli
  • 750
  • 4
  • 13
  • 29
  • 1
    eval() may work, but it is not a safe solution (unless one happens to know the exact input and know that, for example, the code doesn't add its own quotes to escape out of your string and do something else). – Brett Zamir Jun 14 '13 at 08:51