---
title: "js 判断字符长度,包括中文"
author: "harbey"
date: "2009-09-29T10:22:37Z"
tags: []
slug: "js"
draft: false
lastmod: "2009-09-29T10:23:44Z"
id: 480136
url: "https://harbey.iteye.com/blog/480136"
---

```java

   /**
			* 得到字符串的字符长度（一个汉字占两个字符长）
			*/
			function getBytesLength(str) {
			   // 在GBK编码里，除了ASCII字符，其它都占两个字符宽
			   return str.replace(/[^\x00-\xff]/g, 'xx').length;
			}

```