|
|

楼主 |
发表于 2023-1-26 09:34
|
显示全部楼层
代码
- <style>
- #papa {
- margin: auto;
- width: 740px;
- font: normal 16px sans-serif;
- color: #000;
- }
- #papa > h2 { text-align: center; }
- #papa > p { margin: 10px 0; }
- #txtbox {
- width: 100%;
- height: 560px;
- padding: 12px;
- box-sizing: border-box;
- font: normal 16px sans-serif;
- }
- #result {
- font: normal 15px/20px monospace;
- }
- </style>
- <div id="papa">
- <h2>中英字数统计</h2>
- <textarea id="txtbox"></textarea>
- <p><input id="okey" type="button" value="统计"/></p>
- <pre id="result"></pre>
- </div>
- <script>
- let calcwords = (str) => {
- //str = str.trim();
- let reg_all = /(.|\n)/g, //所有字符
- reg_bytebj = /[\x00-\xff]/g, //所有半角字符
- reg_char = /\w/g, //所有 \w 字节
- reg_space = /[\u0009\u0020\u3000]/g,
- reg_word = /\b\w+\b/gm, //英文单词
- reg_link = /\w+[-']\w+/g, //英文单词连接符
- reg_links = /\w+[-']\w+[-']\w+/g, //单词内出现多符号
- reg_py = /[a-z]*[āáǎàōóǒòēéěèīíǐìūúǔùüǖǘǚǜńň??][a-z]*/gi, //汉语拼音
- reg_hanz = /[一-龥]/g, //GBK汉字
- reg_hanzbd = /[\u3002\uff1f\uff01\uff0c\u3001\uff1b\uff1a\u300c\u300d\u300e\u300f\u2018\u2019\u201c\u201d\uff08\uff09\u3014\u3015\u3010\u3011\u2014\u2026\u2013\uff0e\u300a\u300b\u3008\u3009]/g,
- reg_enbd = /[\u0021-\u0029\u002a-\u002f\u003a-\u003f\u0040\u005b-\u005f\u0060\u007b-\u007e]/g, //英文标点
- reg_dblbyte = /[^\x00-\xff]/g; //所有非ASCII字符
- let num_all = reg_all.test(str) ? str.match(reg_all).length : 0; //全部字符数
- let num_bytebj = reg_bytebj.test(str) ? str.match(reg_bytebj).length : 0; //半角字符
- let num_py = reg_py.test(str) ? str.match(reg_py).length : 0; //汉语拼音
- if(num_py > 0) str = str.replace(reg_py,'');
- let num_char = reg_char.test(str) ? str.match(reg_char).length : 0; //\w单词字符
- let num_space = reg_space.test(str) ? str.match(reg_space).length : 0; //空格
- let num_word = reg_word.test(str) ? str.match(reg_word).length : 0; //正则单词
- let num_link = reg_link.test(str) ? str.match(reg_link).length : 0; //英文单连字符
- let num_links = reg_links.test(str) ? str.match(reg_links).length : 0; //英文多连字符
- let num_hanz = reg_hanz.test(str) ? str.match(reg_hanz).length : 0; //GBK汉字
- let num_hanzbd = reg_hanzbd.test(str) ? str.match(reg_hanzbd).length: 0; //汉语标点符号
- let num_enbd = reg_enbd.test(str) ? str.match(reg_enbd).length : 0; //英文标点
- let num_dblbyte = reg_dblbyte.test(str) ? str.match(reg_dblbyte).length : 0; //多字节字符
- let num_line = str.split('\n').length; //行数
- return {
- all: num_all + num_line - 1, //字符数
- bytes: num_bytebj + num_dblbyte * 2 + num_line - 1, //字节数
- en: num_word - num_link - num_links, //英文单词
- en_bd: num_enbd - num_link - num_links, //英文标点
- cn: num_hanz, //GBK汉字
- cn_bd: num_hanzbd, //汉语标点
- py: num_py, //汉语拼音
- space: num_space, //空格
- line: num_line, //行
- other: num_dblbyte - num_hanz - num_hanzbd, //其他双字节字符
- };
- };
- okey.onclick = () => {
- let O = calcwords(txtbox.value);
- result.innerText = `字符数 ${O.all}
- 字节数 ${O.bytes}
- 行 ${O.line}
- 汉字 ${O.cn}
- 单词 ${O.en}
- 其他 中文标点 ${O.cn_bd}
- 英文标点 ${O.en_bd}
- 其他双字节字符 ${O.other}
- 汉语拼音 ${O.py}
- 空格 ${O.space}
- `;
- }
- </script>
复制代码
|
|