zoukankan      html  css  js  c++  java
  • js 统计单词与汉字总个数

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <title>Document</title>
        <textarea id="textarea" style="height: 50vh; 50vh;"></textarea>
        <button onclick="checkWord()">检测</button>
    </head>
    
    <body>
    
    </body>
    <script>
        function checkWord(e) {
            var obj = document.getElementById('textarea').value.replace(/\s+/g,"");
            var str = this.scount(obj)
            console.log(str)
        }function scount(str) {
            var i = 0,
                j = 0,
                c = 0,
                k = 0;
            var t = /[a-zA-Z]+/;
            var bo = false;
            var ao = true;
            for (i = 0, j = i + 1; j <= str.length; i = j++) {
                if (t.test(str.substring(i, j)) && !bo) {
                    bo = true;
                    c++;
                } else if (!t.test(str.substring(i, j))) {
                    bo = false;
                }
            }
            for (i = 0, j = i + 1; j <= str.length; i = j++) {
                if (t.test(str.substring(i, j)) && !bo) {
                    bo = true;
                } else if (!t.test(str.substring(i, j))) {
                    bo = false;
                    k++;
                }
            }
            return c+k;
        }
        
    </script>
    
    </html>
    老鸟也是从菜鸟开始的!
  • 相关阅读:
    css3文字单位rem 设置文字大小
    JS实现多物体width缓冲运动实例
    vs 你不得不会的调试方式
    C# 常用修饰符
    富文本编辑器tinymce
    Swagger简单实例
    marquee标签详解
    table数据跑马灯效果
    SqlServer发布订阅
    ORM概述及常用ORM框架
  • 原文地址:https://www.cnblogs.com/zhangpooo/p/15508849.html
Copyright © 2011-2022 走看看