max max
max max

Reputation: 55

How to decode Arabic characters using JavaScript (Only client side solution)

I have this text:

var txt = 'u0633\u0644\u0627\u0645'; // سلام

and it means: سلام in Arabic.

I need a JavaScript function that will transform this: u0633\u0644\u0627\u0645 to this: سلام for example:

function convert(str){
// code here that convert each letter to Arabic for example or another decode solution ...
return  new_str;
}

var txt = 'u0633\u0644\u0627\u0645'; // سلام

console.log( convert(txt ) ); // سلام

I search for the problem here but all what I get was server side solutions like PHP encoding functions and MySQL connection encode and HTML meta encode (not work also).

Upvotes: 1

Views: 1334

Answers (1)

Mohsen Alyafei
Mohsen Alyafei

Reputation: 5547

Here is one method of doing that:

let txt = '\u0633\u0644\u0627\u0645'; // سلام
console.log( convert(txt ) );         // سلام

function convert(str) {
return decodeURIComponent(JSON.parse('"'+str.replace(/\"/g,'\\"')+'"'));
}

Upvotes: 1

Related Questions