我怎样才能使它计算字符而不是单词

How can I make this count characters insted of words

本文关键字:字符 单词 计算      更新时间:2023-09-26

如何使此代码计数字符而不是单词。

谢谢

function word_count(){
    text_value=CKEDITOR.instances.text_editor_name.getData();
    var matches = text_value.replace(/<[^<|>]+?>| /gi,' ').match(/'b/g);
    var count = 0;
    if(matches) {
        count = matches.length/2;
    }
    document.getElementById("word_count").innerHTML=count+" words";
}
你可以

使用这个 - text_value.length

修改了您的函数 -

function word_count(){
    text_value=CKEDITOR.instances.text_editor_name.getData();
    document.getElementById("word_count").innerHTML=text_value.length+" words";
}

在此函数中,您可以使用text_value.length;而不是现在使用的内容。

您可以使用以下代码来获取字符串的长度。假设您在替换字符后计数。.

var count= text_value.replace(/<[^<|>]+?>| /gi,' ').match(/'b/g);
var result = count.length;
document.getElementById("word_count").innerHTML=result +" characters";

取自 http://www.hscripts.com/tutorials/javascript/string-length-method.php