简体   繁体   English

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

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

I have a script that counts the characters in each of my comments, excluding any Html Tags. 我有一个脚本,可以计算每个注释中的字符, 不包括任何Html标签。

But it doesn't take into account that my comments contain åäöÅÄÖ (swedish letters). 但这并没有考虑到我的评论中包含åäöÅÄÖ (瑞典字母)。 So how do I edit this to "exclude" these from the regexp variable? 那么,如何编辑它以将它们从regexp变量中“排除”? (If the comment is "Hej då!" the result is 6, not 7.) (如果注释为“ Hejdå!”,则结果为6,而不是7。)

Why I need this is a long story, the problem here is in the expression, not that I could use CSS and set a max-height and overflow. 我为什么需要这是一个很长的故事,这里的问题出在表达式上,而不是我可以使用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");
 };
});

There's no need to use a regular expression here. 此处无需使用正则表达式。 This should work: 这应该工作:

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

This works, but includes any and all white-space 这可行,但包含所有空格

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

As it was pointed out to me, this script above will work on swedish letters, although it includes white-space. 正如我所指出的,上面的脚本可以在瑞典字母上使用, 尽管它包含空格。 To avoid that, and as an alternative for swedish text, I ended up using this script below. 为了避免这种情况,并作为瑞典文本的替代方法,我最终在下面使用了此脚本。 It strips out the html first, then uses text().length together with RegEx to include all common swedish letters, along with typical code letters like { [ ( ) ] } if your comments contain lots of that. 它首先去除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