Reputation:
so i have an arabic string then i encode it using encodeURIComponent, then i try to know the length from the encoded string but this code don't work why? http://jsfiddle.net/mCwaj/
var str="قال على";
var encd=encodeURIComponent(str);
alert(encd);
alert(custom_length(encd));
function custom_length(str){
var tab=str.match(/%../g);
return tab.length;
}
the result should be 7 but function returns 13, what i know is that an arabic encoded alphabet is composed like %(letter|number)(letter|number)
Upvotes: 1
Views: 244
Reputation: 598
try using "escape()" instead of "encodeURIComponent()"
//14 charachters
var str="مرحبا أنا ياسر";
var result=custom_length(escape(str));
alert(result); //it'll display 14
function custom_length(str){
var tab=str.match(/%../g);
return tab.length;
}
Demo:http://jsfiddle.net/ysinjab/KDyhp/
Upvotes: 0