我尝试比较两个字符串,第一个 ASCII(英文)和第二个 Unicode(中文)。用户需要在 textarea 中输入消息,javascript 将计算文本的长度并显示一些消息。
现在它仅适用于 ASCII(英语),但我需要比较,因为 ASCII 和 Unicode 的条件不同。
以下是我当前的代码片段:
$('#myinput').keyup(function() {
    if(this.value.length<=152)
     $('#charcount').text('We will deduct 1 credit for this message per contact'), 
     $('#credit_value').val('1');
    else if(this.value.length>152 && this.value.length<298)
         $('#charcount').text('We will deduct 2 credit for this message per contact'),  
         $('#credit_value').val('2');
    else if (this.value.length>=298 && this.value.length<450)
        $('#charcount').text('We will deduct 3 credit for this message per contact'),
        $('#credit_value').val('3');
    else if (this.value.length>=450 && this.value.length<596)
        $('#charcount').text('We will deduct 4 credit for this message per contact'),   
        $('#credit_value').val('4');
    else if (this.value.length>=602 && this.value.length<754)
        $('#charcount').text('We will deduct 5 credit for this message per contact'),  
        $('#credit_value').val('5');
    })
</script>
对于 ASCII(英文),我以 152 开头,但在 Unicode(中文)中,我需要以 60 开头并增加...
下面是我在 php 中使用的代码来检查英文或中文的文本/字符串
// check if message is type in chinese or english/UTF8
    if (preg_match("/\p{Han}+/u", $message)) {
        $msg_type = '2';
        //this is chinese
    } else {
        $msg_type = '1';
        //this is UTF8/English
    }
所以我的问题现在我需要检查字符串是 ASCII 还是 Unicode 类型,然后是中文开始条件值是 60 还是英文是 152 开始。
在这里检查我的 jsfiddle