下面的代码是以字节为单位获得字符串的长度。
<html
<head>
</head>
<body>
<script>
String.prototype.lenB=function()
{
return this.replace(/[^\x0-\xf]/g,"##").length;
}
var sName="a聪慧b";
alert(sName.lenB());
</script>
</body>
</html>上面的代码是我直接从书上搞下来的,我总觉得这代码有点问题。
\x0-\xf 这十六进制编码代表什么东西啊。。一直没查到。请教大家 ,谢谢。
<html
<head>
</head>
<body>
<script>
String.prototype.lenB=function()
{
return this.replace(/[^\x0-\xf]/g,"##").length;
}
var sName="a聪慧b";
alert(sName.lenB());
</script>
</body>
</html>上面的代码是我直接从书上搞下来的,我总觉得这代码有点问题。
\x0-\xf 这十六进制编码代表什么东西啊。。一直没查到。请教大家 ,谢谢。
表示十六进制啊.....
在js中
其实 你这个 js语法是错的 只是在执行的时候 他会 默认为 [^\x00-\xf0]
这样就明白为什么是匹配双字节了吧???
原因不明。