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;width: 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>
老鸟也是从菜鸟开始的!