如何计算编码字符串的长度? [英] How to calculate the length of an encoded string?
问题描述
所以我有一个阿拉伯字符串,然后我使用 encodeURIComponent 对其进行编码,然后我尝试从编码的字符串中知道长度,但是这段代码不起作用,为什么?http://jsfiddle.net/mCwaj/
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;
}
结果应该是 7 但函数返回 13,我所知道的是阿拉伯语编码的字母表的组成类似于 %(letter|number)(letter|number)
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)
推荐答案
您将非编码的 str
而不是 encd
传递给您的函数.因此,正则表达式不匹配和结果 null
在访问其 length
属性时抛出异常.
You are passing the non-encoded str
to your function instead of encd
. Therefore, the regex does not match and the result null
throws an exception on accessing its length
property.
这篇关于如何计算编码字符串的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!