js区分における英語統計文字数


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>js           </title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
    <input type="text" value="" id="str"><span id="showcontent"></span><!--        label  ,     innerHTML   ie8   ,   span   -->
    <script type="text/javascript">
    var countnums=(function(){
        var trim=function(strings){
            return (strings||"").replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,"");//+         ,|    ,\s \u00A0      ,/^ ……  ,$ ……  ,/g    ,/i     
        }
        return function(_str){
            _str=trim(_str);   //            
            var strlength=_str.length;
            if(!strlength){   //         ,   
                return 0;
            }
            var chinese=_str.match(/[\u4e00-\u9fa5]/g); //    ,match         
            return strlength+(chinese?chinese.length:0); //      

        }
    })();
    function count(tThis){
        var charnum=countnums(tThis.value)
            var showid=document.getElementById("showcontent");
            showid.innerHTML="      "+charnum+"   ";
            
    }
    window.onload=function(){
        var str=document.getElementById("str");
        str.onkeypress=function(){
            count(this);
        }
        str.onkeyup=function(){
            count(this);
        }
    }
    </script>
</body>
</html>