如何计算编码字符串的长度? [英] How to calculate the length of an encoded string?

查看:445
本文介绍了如何计算编码字符串的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个阿拉伯字符串,然后我使用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.因此,正则表达式不会 match 和结果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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆