gpt4 book ai didi

javascript - 删除文本区域中的重复文本

转载 作者:行者123 更新时间:2023-11-28 15:24:14 25 4
gpt4 key购买 nike

在这个网站上一些人的帮助下,我设法在这里创建了一个主题标签计数器 - https://codepen.io/timothycdavis17/pen/ZJeVBj .

这是代码。

HTML

<section class="section-textarea bg-primary">

<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="well textarea-well clearfix">
<textarea rows="16" class="form-control"></textarea>

<div class="buttons">
<div class="btn-group" role="group" aria-label="Basic example">

<button type="button" class="btn btn-secondary btn-primary btn-lg
count-button">Count</button>

<button type="button" class="btn btn-secondary btn-danger btn-lg
reset-button">Reset</button>

</div>

</div>

<div class="remaining-counter">Hashtags Counted:&nbsp;&nbsp;<span
class="well text-well">0</span></div>


</div>

</div>

</div>

</div>

</section>

CSS

body {
background: #0275D8;
}

.textarea-well {
color: #000;
text-align: center;
margin: 10% auto;
}

textarea {

margin-bottom: 20px;

}

.text-well {
background: lightblue;
}

.count-button {
margin-bottom: 20px;
}

.remaining-counter {
font-size: 30px;
margin-bottom: 20px;
}

jQuery

$(".count-button").click(function() {
var text = $("textarea").val();
var count = (text.match(/(^|\W)(#[a-z\d A-Z\D][\w-]*)/g) || []).length;
$('.text-well').html(count);

var seen = {};
$('textarea').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});

}).trigger('click');


$(".reset-button").click(function() {
$("textarea").val(' ');
$('.text-well').html(0);
}).trigger('click');

制作完成后,我被问到是否可以不计算输入到文本区域中的任何重复主题标签,并且可能会错误地用红色波浪线为主题下划线。

我浏览了这个网站以寻找答案,但似乎找不到有效的答案。

感谢任何帮助。

最佳答案

只需使用一个临时数组来过滤掉重复项。

// save the matches instead of immediately counting
var tags = text.match(/(^|\W)(#[a-z\d A-Z\D][\w-]*)/g) || [];
// init empty array for holding unique matches
var uTags = [];
// loop through matches
$.each(tags, function(index, value){
// if match is not in holding array array, add it to holding array
if(uTags.indexOf(value) == -1) uTags.push(value);
});
// get count from holding array which will only have one copy of each match
var count = uTags.length;

Here is a fork of your codepen with the above code.

关于javascript - 删除文本区域中的重复文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45576083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com