Javascript 删除Textarea中的重复文本
在这个网站上一些人的帮助下,我设法在这里创建了一个标签计数器- 这是代码 HTMLJavascript 删除Textarea中的重复文本,javascript,jquery,html,css,Javascript,Jquery,Html,Css,在这个网站上一些人的帮助下,我设法在这里创建了一个标签计数器- 这是代码 HTML <section class="section-textarea bg-primary"> <div class="container"> <div class="row"> <div class="col-lg-12"> <div class="well textarea-well clearfix">
<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: <span
class="well text-well">0</span></div>
</div>
</div>
</div>
</div>
</section>
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;
非常感谢,有没有一种方法可以设置重复文本的样式以向用户显示它是重复的?当然,可以在if语句中添加一个else块,并添加要在duplicate Found上运行的代码我已经尝试添加一个else语句,但老实说,我并不完全确定我在做什么。我已经更新了我的代码。这个问题开始超出原来问题的范围。但是,我建议将DUP推送到dupArray,然后将该数组的内容输出到计数器周围的某个位置。您将无法在textarea中设置实际标签的样式。
// 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;