|
|

楼主 |
发表于 2023-1-22 21:24
|
显示全部楼层
本帖最后由 马黑黑 于 2023-1-22 21:26 编辑
二楼代码:
- <style>
- #txtbox {
- width: 740px;
- height: 520px;
- padding: 12px;
- display: block;
- margin: auto;
- }
- </style>
- <textarea id="txtbox"></textarea>
- <p><br><input id="okey" type="button" value="统计"/> <span id="result" ></span>
- <script>
- let calcwords = (str) => {
- let reg_word = /\b\w+\b/gm,
- reg_link = /\w+[-']\w+/g,
- reg_links = /\w+-\w+-\w+/g,
- reg_hanz = /[一-龥]/g;
- let num_word = 0,
- num_link = 0,
- num_links = 0,
- num_hanz = 0;
- if (reg_word.test(str)) num_word = str.match(reg_word).length;
- if (reg_link.test(str)) num_link = str.match(reg_link).length;
- if(reg_links.test(str)) num_links = str.match(reg_links).length;
- console.log(reg_link.test(str));
- if (reg_hanz.test(str)) num_hanz = str.match(reg_hanz).length;
- return {
- en: num_word - num_link - num_links,
- cn: num_hanz,
- };
- };
- okey.onclick = () => {
- let a = calcwords(txtbox.value);
- result.innerText = `英文单词 ${a.en} | 汉字 ${a.cn}`;
- }
- </script>
复制代码
|
|