Count characters in P while keeping non-Latin characters

I have a script that counts characters in each of my comments, excluding any HTML tags.

But he doesn't take into account that my comments contain åöÅÄÄ (Swedish letters). So how do I change this to "exclude" them from the regexp variable ? (If the comment is "Hej då!", The Result is 6, not 7.)

Why I need this is a long story, the problem here is the expression and not that I can use CSS and set max height and overflow. :)

// 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");
 };
});

      

+2


source to share


2 answers


There is 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");
    }
});

      

+4


source


This works, but includes all and all spaces

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

      



As pointed out to me, this script above will work in swedish letters, although it includes white space. To avoid this, and as an alternative for Swedish text, I ended up using this script below. It preempts the html first and then uses text (). Length along with RegEx to include all normal Swedish letters, along with typical code letters such as {[()]} if your comments contain a lot.

$("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");
    };
});

      

+1


source







All Articles