发布网友 发布时间:2022-05-01 09:28
共2个回答
热心网友 时间:2022-06-27 13:08
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>统计英文字母和中文个数</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <script> function myCount() { var content = document.getElementById("myText").value; //匹配英文字母 var reg1 = /[a-zA-z]/g; var letterCount = 0; if (content.match(reg1) != null) { letterCount = content.match(reg1).length; } //匹配数字 var reg2 = /\d/g; var numberCount = 0; if (content.match(reg2) != null) { numberCount = content.match(reg2).length; } //匹配中文 var reg3 = /[^\u4e00-\u9fa5]/g; var englishCount = 0; if (content.match(reg3) != null) { englishCount = content.match(reg3).length; var chineseCount = content.length - englishCount - numberCount; } var result = "英文字母的个数为:" + letterCount + "<br /><br />数字的个数为:" + numberCount + "<br /><br />中文的个数:" + chineseCount; document.getElementById("result").innerHTML = result; } </script> </HEAD> <BODY> <input type="text" id="myText" style="width: 300px;" /> <input type="button" id="verify" value="统计" onclick="myCount();" /> <br> <br> <h3> 结果: </h3> <div id="result" style="width: 300px; padding: 10px; overflow: hidden;"></div> </BODY> </HTML>热心网友 时间:2022-06-27 13:08
把字符串分隔下,用acsii码区间来判断