Javascript jQuery删除重复的hashtag

Javascript jQuery删除重复的hashtag,javascript,jquery,regex,Javascript,Jquery,Regex,我试图使用jQuery创建一个hashtag系统,但是我的代码有问题。因此,通常代码工作正常。但是我想删除写入的hashtags中的重复标记 我在codepen.io上创建了这个 在本演示中,当您编写内容并按空格键时,会自动添加到单词前面。我不希望用户能够像这样两次书写同一个单词:#abc#abc#abc#abc#abc#abc 我使用正则表达式创建了这段代码,但它不起作用 // Remove duplicated hashatgs. text = text.replace(/[#]{2,}

我试图使用jQuery创建一个hashtag系统,但是我的代码有问题。因此,通常代码工作正常。但是我想删除写入的hashtags中的重复标记

我在codepen.io上创建了这个

在本演示中,当您编写内容并按空格键时,
会自动添加到单词前面。我不希望用户能够像这样两次书写同一个单词:
#abc#abc#abc#abc#abc#abc

我使用正则表达式创建了这段代码,但它不起作用

// Remove duplicated hashatgs.
  text = text.replace(/[#]{2,}/gi, function removeHashtags(x) { return '#'; });
以下是完整代码:

//将光标移到末尾。
函数placeCaretAtEnd(el){
el.focus();
if(typeof window.getSelection!=“未定义”
&&typeof document.createRange!=“未定义”){
var range=document.createRange();
范围。选择节点内容(el);
范围。塌陷(假);
var sel=window.getSelection();
选择removeAllRanges();
选择添加范围(范围);
}else if(typeof document.body.createTextRange!=“未定义”){
var textRange=document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
//在每个单词上添加hashtag。
函数addHashtags(文本){
//添加标签。
text=text.replace(/([\w]+)/gi,函数addHashtag(x){return'#'+x;});
//删除重复的哈希值。
text=text.replace(/[#]{2,}/gi,函数removeHashtags(x){return'#';});
//防止双空格
text=text.replace(//g',);
text=text.trim();
//返回结果
返回文本;
}
//将字符转换为字符码
函数toCharCode(char){返回char.charCodeAt(0);}
//定义特殊字符:
变量字符=新集合([
0,32,//空格
13,//输入
//添加其他标点符号或键
]);
var禁止字符=新集合([
toCharCode(“”),
toCharCode(“-”),
toCharCode(“?”),
toCharCode(“*”),
toCharCode(\\”,
toCharCode(“/”),
toCharCode(“”),
toCharCode(“)”,
toCharCode(“=”),
toCharCode(“&”),
toCharCode(“%”),
toCharCode(“+”),
toCharCode(“^”),
toCharCode(“#”),
toCharCode(“”),
toCharCode(“”),
toCharCode(“.”),
toCharCode(“,”),
toCharCode(“;”)
]);
$(文档).ready(函数(){
var元素='#text';
$(元素)。按键(功能(e){
if(字符数.has(例如keyCode)){
//获取并修改文本。
var text=$(元素).text();
text=addHashtags(text);
//保存内容。
$(元素)。文本(文本);
//将光标移到末尾
placeCaretAtEnd(document.querySelector(元素));
}else if(禁止字符.has(例如keyCode)){
e、 预防默认值();
}
});
});
.container{
位置:相对位置;
宽度:100%;
最大宽度:600px;
溢出:隐藏;
填充:10px;
保证金:0px自动;
边缘顶部:50px;
}
* {
框大小:边框框;
-webkit框大小:边框框;
-moz框大小:边框框;
}
艾迪兹先生{
位置:相对位置;
宽度:100%;
填充:30px;
边框:1px实心#d8dbdf;
大纲:无;
字体系列:helvetica、arial、无衬线字体;
-moz osx字体平滑:灰度;
-webkit字体平滑:抗锯齿;
}
.addiez::-webkit输入占位符{
/*Chrome/Opera/Safari*/
颜色:rgb(0,0,1);
}
.addiez[contentEditable=true]:空:不(:焦点):在{
内容:attr(占位符);
颜色:#444;
}
.注{
位置:相对位置;
宽度:100%;
填充:30px;
字体大小:300;
字体系列:helvetica、arial、无衬线字体;
-moz osx字体平滑:灰度;
-webkit字体平滑:抗锯齿;
线高:1.8rem;
字体大小:13px;
}
.广告文本{
位置:相对位置;
宽度:100%;
填充:10px 30px;
溢出:隐藏;
字体大小:300;
字体系列:helvetica、arial、无衬线字体;
-moz osx字体平滑:灰度;
-webkit字体平滑:抗锯齿;
线高:1.8rem;
字体大小:13px;
}

您可以做的一件事是,在
#
处拆分并从数组中删除重复项,然后再次使用
#
加入数组。。通过这样做:

var unique = text.replace(/\s/g,'').split('#').filter(function(elem, index, self) {
return index == self.indexOf(elem.replace('\s+$',''));
})
text = unique.join(" #");

您可以做的一件事是,在
#
处拆分并从数组中删除重复项,然后再次使用
#
加入数组。。通过这样做:

var unique = text.replace(/\s/g,'').split('#').filter(function(elem, index, self) {
return index == self.indexOf(elem.replace('\s+$',''));
})
text = unique.join(" #");

您可以这样修改AddHashtags函数:

// Add hashtags on each word.
function addHashtags(text) {
    // Add hashtag.
    text = text.replace(/([\w]+)/gi, function addHashtag(x){ return '#' + x; });

    // Remove duplicated words
    text = text.replace(/\b(\w+)\b(?=.*\b\1\b)/gi, function removeDuplicates(x) { return ''; });

    // Prevent single/double hashtags
    text = text.replace(/[#]{2,}/gi, function removeDoubleHashtags(x) { return '#'; });
    text = text.replace(/[#]{1}\s+/gi, function removeSingleHashtags(x) { return ''; });

    // Prevent double spaces  
    text = text.replace(/  /g, ' ');
    text = text.trim();

    // return results
    return text;
}
如果有重复的单词,它将覆盖原始条目,因此如果列表看起来像:#hashtag#hashtag,那么#hashtag将是剩下的条目。如果您正在寻找,那么包含确保所有内容都是小写的代码应该很容易


用于删除列表中重复单词的正则表达式如下所示:

您可以像这样修改AddHashtags函数:

// Add hashtags on each word.
function addHashtags(text) {
    // Add hashtag.
    text = text.replace(/([\w]+)/gi, function addHashtag(x){ return '#' + x; });

    // Remove duplicated words
    text = text.replace(/\b(\w+)\b(?=.*\b\1\b)/gi, function removeDuplicates(x) { return ''; });

    // Prevent single/double hashtags
    text = text.replace(/[#]{2,}/gi, function removeDoubleHashtags(x) { return '#'; });
    text = text.replace(/[#]{1}\s+/gi, function removeSingleHashtags(x) { return ''; });

    // Prevent double spaces  
    text = text.replace(/  /g, ' ');
    text = text.trim();

    // return results
    return text;
}
如果有重复的单词,它将覆盖原始条目,因此如果列表看起来像:#hashtag#hashtag,那么#hashtag将是剩下的条目。如果您正在寻找,那么包含确保所有内容都是小写的代码应该很容易

用于删除列表中重复单词的正则表达式来自此SO答案: