Discuz教程网

js统计中文字符个数的函数

[复制链接]
authicon dly 发表于 2011-4-18 13:56:41 | 显示全部楼层 |阅读模式
统计文本中中文字符个数的函数,代码如下:
  1. <script>
  2. // 统计汉字数,不包括标点符号
  3. function countChinese(str){
  4. var m=str.match(/[\u4e00-\u9fff\uf900-\ufaff]/g);
  5. return (!m?0:m.length);
  6. }
  7. // 统计非ASCII字符数
  8. function countNonAlphabet(str){
  9. var m=str.match(/[^\x00-\x80]/g);
  10. return (!m?0:m.length);
  11. }
  12. var string="Make a test: 这是晴枫的博客——枫芸志!";
  13. alert(countChinese(string));
  14. alert(countNonAlphabet(string));
  15. </script>
复制代码






上一篇:Discuz X1.5聊天室插件
下一篇:不允许回复纯数字、纯字母、纯表情,须回复中文,中文字数可设置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-1 23:41

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表