jQuery 删除字符串中出现的重复单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16843991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove occurrences of duplicate words in a string
提问by CLiown
Take the following string as an example:
以下面的字符串为例:
var string = "spanner, span, spaniel, span";
From this string I would like to find the duplicate words, remove all the duplicates keeping one occurrence of the word in place and then output the revised string.
从这个字符串中,我想找到重复的单词,删除所有重复的单词,保留一个单词出现的位置,然后输出修改后的字符串。
Which in this example would be:
在这个例子中是:
var string = "spanner, span, spaniel";
I've setup a jsFiddle for testing: http://jsfiddle.net/p2Gqc/
我已经设置了一个 jsFiddle 进行测试:http: //jsfiddle.net/p2Gqc/
Note that the order of the words in the string is not consistent, neither is the length of each string so a regex isn't going to do the job here I don't think. I'm thinking something along the lines of splitting the string into an array? But I'd like it to be as light on the client as possible and super speedy...
请注意,字符串中单词的顺序不一致,每个字符串的长度也不一致,因此我认为正则表达式不会在这里完成这项工作。我正在考虑将字符串拆分为数组的方法?但我希望它对客户端尽可能轻,而且速度超快......
回答by PSL
How about something like this?
这样的事情怎么样?
split the string, get the array, filter it to remove duplicate items, join them back.
拆分字符串,获取数组,对其进行过滤以删除重复项,然后将它们重新连接起来。
var uniqueList=string.split(',').filter(function(item,i,allItems){
return i==allItems.indexOf(item);
}).join(',');
$('#output').append(uniqueList);
Fiddle
小提琴
For non supporting browsers you can tackle it by adding this in your js.
对于不支持的浏览器,您可以通过在 js 中添加它来解决它。
See Filter
见过滤器
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
"use strict";
if (this == null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}
回答by Hirad Nikoo
If non of the above works for you here is another way:
如果以上都不适合你,这里是另一种方式:
var str = "spanner, span, spaniel, span";
str = str.replace(/[ ]/g,"").split(",");
var result = [];
for(var i =0; i < str.length ; i++){
if(result.indexOf(str[i]) == -1) result.push(str[i]);
}
result=result.join(", ");
Or if you want it to be in a better shape try this:
或者,如果您希望它处于更好的状态,请尝试以下操作:
Array.prototype.removeDuplicate = function(){
var result = [];
for(var i =0; i < this.length ; i++){
if(result.indexOf(this[i]) == -1) result.push(this[i]);
}
return result;
}
var str = "spanner, span, spaniel, span";
str = str.replace(/[ ]/g,"").split(",").removeDuplicate().join(", ");
回答by Niket Pathak
Alternate Solution using Regular Expression
使用正则表达式的替代解决方案
By making use of positive lookahead, you can strip off all the duplicate words.
通过使用积极的前瞻,您可以去除所有重复的单词。
Regex /(\b\S+\b)(?=.*\1)/ig
, where
正则表达式/(\b\S+\b)(?=.*\1)/ig
,其中
\b
- matches word boundary\S
- matches character that is not white space(tabs, line breaks,etc)?=
- used for positive lookaheadig
- flags for in-casesensitive,global search respectively+,*
- quantifiers. + -> 1 or more, * -> 0 or more()
- define a group\1
- back-reference to the results of the previous group
\b
- 匹配词边界\S
- 匹配非空白字符(制表符、换行符等)?=
- 用于正向预测ig
- 分别用于区分大小写、全局搜索的标志+,*
- 量词。+ -> 1 或更多,* -> 0 或更多()
- 定义一个组\1
- 反向引用上一组的结果
var string1 = 'spanner, span, spaniel, span';
var string2 = 'spanner, span, spaniel, span, span';
var string3 = 'What, the, the, heck';
// modified regex to remove preceding ',' and ' ' as per your scenario
var result1 = string1.replace(/(\b, \w+\b)(?=.*)/ig, '');
var result2 = string2.replace(/(\b, \w+\b)(?=.*)/ig, '');
var result3 = string3.replace(/(\b, \w+\b)(?=.*)/ig, '');
console.log(string1 + ' => ' + result1);
console.log(string2 + ' => ' + result2);
console.log(string3 + ' => ' + result3);
The only caveat is that this regex keeps only the last instance of a found duplicate word and strips off all the rest. To those who care only about duplicates and not about the order of the words, this should work!
唯一需要注意的是,这个正则表达式只保留找到的重复单词的最后一个实例,并去掉所有其余的。对于那些只关心重复而不关心单词顺序的人来说,这应该有效!
回答by gdoron is supporting Monica
回答by codebox
Both the other answers would work fine, although the filter
array method used by PSL was added in ECMAScript 5 and won't be available in old browsers.
尽管filter
PSL 使用的数组方法是在 ECMAScript 5 中添加的,并且在旧浏览器中不可用,但其他两个答案都可以正常工作。
If you are handling long strings then using $.inArray
/Array.indexOf
isn't the most efficient way of checking if you've seen an item before (it would involve scanning the whole array each time). Instead you could store each word as a key in an object and take advantage of hash-based look-ups which will be much faster than reading through a large array.
如果您正在处理长字符串,那么使用$.inArray
/Array.indexOf
并不是检查您之前是否看过某个项目的最有效方法(每次都需要扫描整个数组)。相反,您可以将每个单词作为键存储在对象中,并利用基于哈希的查找,这将比读取大型数组快得多。
var tmp={};
var arrOut=[];
$.each(string.split(', '), function(_,word){
if (!(word in tmp)){
tmp[word]=1;
arrOut.push(word);
}
});
arrOut.join(', ');
回答by anmml
To delete all duplicate words, I use this code:
要删除所有重复的单词,我使用以下代码:
<script>
function deleteDuplicate(a){a=a.toString().replace(/ /g,",");a=a.replace(/[ ]/g,"").split(",");for(var b=[],c=0;c<a.length;c++)-1==b.indexOf(a[c])&&b.push(a[c]);b=b.join(", ");return b=b.replace(/,/g," ")};
document.write(deleteDuplicate("g g g g"));
</script>
回答by Ashwini Singh
<script type="text/javascript">
str=prompt("Enter String::","");
arr=new Array();
arr=str.split(",");
unique=new Array();
for(i=0;i<arr.length;i++)
{
if((i==arr.indexOf(arr[i]))||(arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])))
unique.push(arr[i]);
}
unique.join(",");
alert(unique);
</script>
this code block will remove duplicate words from a sentence.
此代码块将从句子中删除重复的单词。
the first condition of if statement i.e (i==arr.indexOf(arr[i])) will include the first occurence of a repeating word to the result(variale unique in this code).
if 语句的第一个条件即 (i==arr.indexOf(arr[i])) 将包括重复单词的第一次出现到结果中(变量在此代码中是唯一的)。
the second condition (arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])) will include all non repeating words.
第二个条件 (arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])) 将包括所有非重复单词。
回答by praveenak
below is an easy to understand and quick code to remove duplicate words in a string:
下面是一个易于理解且快速的代码,用于删除字符串中的重复单词:
var string = "spanner, span, spaniel, span";
var uniqueListIndex=string.split(',').filter(function(currentItem,i,allItems){
return (i == allItems.indexOf(currentItem));
});
var uniqueList=uniqueListIndex.join(',');
alert(uniqueList);//Result:spanner, span, spaniel
As simple as this can solve your problem. Hope this helps. Cheers :)
就这么简单,就可以解决你的问题。希望这可以帮助。干杯:)
回答by Praveen Kumar
var string = "spanner, span, spaniel, span";
var strArray= string.split(",");
var unique = [];
for(var i =0; i< strArray.length; i++)
{
eval(unique[strArray] = new Object());
}
//You can easily traverse the unique through foreach.
//可以通过foreach轻松遍历unique。
I like this for threereason. First, it works with IE8 or any other browser.
我喜欢这个有三个原因。首先,它适用于 IE8 或任何其他浏览器。
Second. it is more optimized and guaranteed to have unique result.
第二。它更加优化并保证具有独特的结果。
Last, It works for Other String array which has White space in their inputs like
最后,它适用于其他字符串数组,其输入中有空格,例如
var string[] = {"New York", "New Jersey", "South Hampsire","New York"};
for the above case there will be only three elementsin the string[] which would be uniquely stored.
对于上述情况,string[]中将只有三个元素将被唯一存储。