如何计算包含标签的文本区域中的字符数

发布于 2024-10-08 18:38:29 字数 1915 浏览 0 评论 0原文

我希望能够计算文本区域中的字符数,并在字符用完时向客户报告。我已经做得很好,但现在我需要添加更多功能,但我不知道从哪里开始。

基本上,除了普通字符之外,客户还可以通过按钮将标签添加到文本框(这已经完成)。 tagg 按以下格式插入到内容中:

一些随机内容 [tag] 以及更多内容

我希望能够测量字符数,并且无论 [tag] 出现在哪里,都会在总数中添加 20 个字符。当删除[标签]或[标签]的一部分时,它还可以删除附加字符。

我有一个基本版本可以工作,但它无法确定标签何时被删除。

function countCharacters(totalTags){

    var new_length = $('#t_text').val().length;
    if (new_length < 10){
        totalTags=0;    
    }
    additionalcharacters = totalTags*10;
    var new_length = $("#t_text").val().length+additionalcharacters;
    if(new_length <= 154) {
        $('#charlimitinfo').removeClass('red');
        $('.cost').html('1');
    } else if(new_length >= 154 && new_length <= 308) {
        $('#charlimitinfo').removeClass('red');
        $('.cost').html('2');
    } else if(new_length >= 308 && new_length <= 462) {
        $('#charlimitinfo').removeClass('red');
        $('.cost').html('3');
    } else if(new_length >= 462 && new_length <= 606) {
        $('#charlimitinfo').removeClass('red');
        $('.cost').html('4');
    } else if(new_length >= 606 && new_length <= 616) {
        $('#charlimitinfo').addClass('red');
        $('.cost').html('5');
    }         
    else {
        if(new_length >= 616){
            $('#t_text').next('#charlimitinfo').addClass('red');
            $('#charlimitinfo').html('You have reached 616 characters!');
            this.value = this.value.substring(0, 616);
            return false;
        }
        else{
            $('#charlimitinfo').removeClass('red');
            $('#charlimitinfo').html((616 - new_length) +'<span>characters left</span>');
            return true;
        }
    }
    $('#charlimitinfo').html(new_length);  
}

t_text = 文本区域 TotalTags = 每次单击标签按钮并插入标签时的计数器。

希望有人能提供帮助,因为这让我的瑞典人很烦恼。

干杯

戴夫

I want to be able to count the number of characters in a textarea, and report out to the customer when they have run out of chracters. I have done that just fine, but now I need to add some more functionallity and I am lost where to start.

Basically along with normal characters the customer will be able to add tags to the textbox from a button (this is already done). The tagg is inserted into the content in the following format:

some random content [tag] with more content

I want to be able to measure the number of characters and wherever a [tag] occurs add 20 characters to the total. It also nneds to remove the additional characters when a [tag] or part of a [tag] is deleted.

I have a basic version working, but it can't figure when a tag is deleted.

function countCharacters(totalTags){

    var new_length = $('#t_text').val().length;
    if (new_length < 10){
        totalTags=0;    
    }
    additionalcharacters = totalTags*10;
    var new_length = $("#t_text").val().length+additionalcharacters;
    if(new_length <= 154) {
        $('#charlimitinfo').removeClass('red');
        $('.cost').html('1');
    } else if(new_length >= 154 && new_length <= 308) {
        $('#charlimitinfo').removeClass('red');
        $('.cost').html('2');
    } else if(new_length >= 308 && new_length <= 462) {
        $('#charlimitinfo').removeClass('red');
        $('.cost').html('3');
    } else if(new_length >= 462 && new_length <= 606) {
        $('#charlimitinfo').removeClass('red');
        $('.cost').html('4');
    } else if(new_length >= 606 && new_length <= 616) {
        $('#charlimitinfo').addClass('red');
        $('.cost').html('5');
    }         
    else {
        if(new_length >= 616){
            $('#t_text').next('#charlimitinfo').addClass('red');
            $('#charlimitinfo').html('You have reached 616 characters!');
            this.value = this.value.substring(0, 616);
            return false;
        }
        else{
            $('#charlimitinfo').removeClass('red');
            $('#charlimitinfo').html((616 - new_length) +'<span>characters left</span>');
            return true;
        }
    }
    $('#charlimitinfo').html(new_length);  
}

t_text = The textarea
totalTags = A counter for each time a tag button is clicked and a tag inserted.

Hope someone can help as this is kettling my swede.

Cheers

Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2024-10-15 18:38:29

有趣的问题。我可能会用正则表达式来处理它,而不是用标签计数器:

function countCharacters()
{
    var text = $('#t_text').val(),
        tags = ['tag', 'tag2'], // An array of your tags
        i,
        matches,
        regex,
        extraCharacters = 0,
        totalCharacters;

    for (i = 0; i < tags.length; i++)
    {
        regex = new RegExp('\\[' + tags[i] + '\\]', 'g');
        matches = text.match(regex) || [];
        extraCharacters += (matches.length * 20) - (matches.length * (tags[i].length + 2));
    }

    totalCharacters = text.length + extraCharacters;

    // Do your if statements/style changes/etc. here
}

这做了几个假设:

  • 每个标签值 20 个字符(如您的帖子中提到的,尽管代码说 10)
  • 标签被替换为 20 个字符
  • 标签实际上在您的文本中(我不明白为什么它们不会)

此解决方案的一大优点是,如果有人手动放入标签,这将捕获它,而您的标签计数器不会。

编辑

代码未经测试,正则表达式可能是贪婪的(首先匹配最长的可能匹配)。现在测试。

编辑 2

好吧,代码有严重错误。弄清楚它是什么。

编辑 3

事实证明,在用字符串创建正则表达式时,必须对特殊字符进行双重转义:new RegExp('\\[' + tag[i] + '\\]' , 'g') - 一次用于字符串,一次用于正则表达式。另外,如果没有找到匹配项,string.matches(regex) 返回 null,因此我们将其与空数组进行“或”运算,并在计算中使用 matches 的长度。

男人。我在编码方面输了。 :-D

Interesting question. I'd probably handle it with regular expressions, rather than with a tag counter:

function countCharacters()
{
    var text = $('#t_text').val(),
        tags = ['tag', 'tag2'], // An array of your tags
        i,
        matches,
        regex,
        extraCharacters = 0,
        totalCharacters;

    for (i = 0; i < tags.length; i++)
    {
        regex = new RegExp('\\[' + tags[i] + '\\]', 'g');
        matches = text.match(regex) || [];
        extraCharacters += (matches.length * 20) - (matches.length * (tags[i].length + 2));
    }

    totalCharacters = text.length + extraCharacters;

    // Do your if statements/style changes/etc. here
}

This makes a couple assumptions:

  • Each tag is worth 20 characters (as mentioned in your post, though the code said 10)
  • The tag is replaced by the 20 characters
  • The tags are actually in your text (I don't see why they wouldn't be)

The big advantage to this solution is that if someone manually puts a tag in, this will catch it, whereas your tag counter would not.

Edit

The code is untested, and the regex may be greedy (matching the longest possible match first). Testing now.

Edit 2

Okay, something is terribly wrong with the code. Figuring out what it is.

Edit 3

Turns out you have to double-escape special characters when making a regex out of a string: new RegExp('\\[' + tags[i] + '\\]', 'g') - once for the string, and once for the regex. Also, string.matches(regex) returns null if no matches are found, so we'll OR it with an empty array, and use the length of matches in our calculation.

Man. I lose at coding. :-D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文