繁体   English   中英

计算P中保留非拉丁字符的字符

[英]Count characters in P keeping non-latin characters

我有一个脚本,可以计算每个注释中的字符, 不包括任何Html标签。

但这并没有考虑到我的评论中包含åäöÅÄÖ (瑞典字母)。 那么,如何编辑它以将它们从regexp变量中“排除”? (如果注释为“ Hejdå!”,则结果为6,而不是7。)

我为什么需要这是一个很长的故事,这里的问题出在表达式上,而不是我可以使用CSS并设置max-height和溢出。 :)

// check if comments are too long
$("ol li p").each(function() {
 var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
 var count = $(this).html().replace(regexp,"").length;
 if ( count >= 620 ) {
  $(this).parent().addClass("too-big-overflow");
 };
});

此处无需使用正则表达式。 这应该工作:

$("ol li p").each(function() {
    var count = $(this).text().length;
    if ( count >= 620 ) {
        $(this).parent().addClass("too-big-overflow");
    }
});

这可行,但包含所有空格

$("ol li p").each(function() {
    var count = $(this).text().length;
    if ( count >= 620 ) {
        $(this).parent().addClass("too-big-overflow");
    }
});

正如我所指出的,上面的脚本可以在瑞典字母上使用, 尽管它包含空格。 为了避免这种情况,并作为瑞典文本的替代方法,我最终在下面使用了此脚本。 它首先去除html,然后将text()。length与RegEx一起使用,以包括所有常见的瑞典字母,以及典型的代码字母,例如{[()]},如果您的注释中包含很多的话。

$("ol li p").each(function() {
    // This removes any tags inside the text like <abbr>, <span> etc
    var regexp = /<[^>]+>/gi;
    var strippedHtml = $(this).text().replace(regexp,"");
    // This counts all common (swedish) letters used, not including the white-space in your html
    lettersCounted = strippedHtml.match(/[a-z0123456789åäö_,éèáà´`'~ ½§£@&%#"-:;<>!\[\]\\\^\$\.\|\?\*\+\(\)\{\}]/gi).length;
    if ( lettersCounted >= 620 ) {
        $(this).parent().addClass("too-big-overflow");
    };
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM