1

所以我有一个阿拉伯字符串,然后我使用 encodeURIComponent 对其进行编码,然后我尝试从编码字符串中知道长度,但是这段代码不起作用,为什么?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;
}

结果应该是 7,但函数返回 13,我知道阿拉伯编码的字母表的组成类似于 %(letter|number)(letter|number)

4

2 回答 2

1

您将非编码传递str给您的函数,而不是encd. 因此,正则表达式不匹配,结果在访问其属性null时引发异常。length

于 2013-03-16T18:49:29.060 回答
0

尝试使用“escape()”而不是“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;
}

演示:http://jsfiddle.net/ysinjab/KDyhp/

于 2013-03-16T19:03:54.580 回答