简体   繁体   English

如何删除字符串中除“ abc”以外的所有匹配项?

[英]How to remove all but one occurrences of 'abc' in a string?

Given the string 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc' , how do I remove all multiple occurrences but one of abc , acb ... 给定字符串'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc' ,我如何删除所有多次出现但只有一个的的abcacb ...

var str = 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc'

remove('abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc') 
//=> 'abc-acb-bac-bca-cab-cba'

I saw this question about removing duplicate characters from a string , but it only addresses repeated letters and I want to target specific substrings. 我看到了有关从字符串中删除重复字符的问题 ,但它仅解决重复的字母,并且我希望定位特定的子字符串。

I would suggest using a split - filter - join combination, using a plain JavaScript object to store the set of substrings ( 'abc' , 'bac' , etc.) encountered so far, and returning false from filter when repeats are found so as to omit them from the result. 我建议使用split - filter - join组合,使用普通的JavaScript对象存储到目前为止遇到的子字符串集( 'abc''bac'等),并在发现重复项时从filter返回false ,例如从结果中忽略它们。

This has the added benefit of running in linear ( O(n) ) time relative to the length of the string, whereas most solutions using indexOf run at the much slower complexity of O(n 2 ) . 这具有相对于字符串长度以线性( O(n) )时间运行的附加好处,而大多数使用indexOf解决方案以O(n 2慢得多的复杂度运行。

 function remove (string) { return string.split('-').filter(function (e) { return !this[e] && (this[e] = true) }, {}).join('-') } console.log(remove('abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc')) 

Edit : For anyone who is confused, the reference to this in the example above refers to the {} passed as the second argument to filter . 编辑 :对于任何困惑的人,在上面的示例中this的引用是指{}作为filter的第二个参数传递

Use split('-') to remove all hyphens and filter() and indexOf() methods to return one of each element (all unique like an ES6 Set) 使用split('-')删除所有连字符,并使用filter()indexOf()方法返回每个元素之一(所有元素都唯一,如ES6集)

Just the mention of ES6 Set reminded me of an easier way to extract unique values from an array. 刚刚提到的ES6 Set提醒了我一个更简单的方法来从数组中提取唯一值。 See Snippet 2. 请参阅摘要2。

SNIPPET 1 片段1

 const str = `abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc`; // Use split on every hyphen const arr = str.split('-'); // Pass arr into function... function arrayUnique(arr) { // Filter arr with the results of... return arr.filter(function(ele, idx, arr) { /* ...the indexOf the current object || for each iteration, return only || if it matches current index */ return arr.indexOf(ele) === idx; }); } const res = arrayUnique(arr); console.log(res); 

SNIPPET 2 片段2

 const str = `abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc`; // split('-') to remove all hyphens const arr = str.split('-'); /* Use the Set Constructor || passing arr through an iterator || converting arr to a Set || a Set can only have unique || values */ const set = new Set(arr); // Do not use the SO Console, use the browser's console console.log(set); 

Try the following: 请尝试以下操作:

 var data = 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc'; var dataArr = data.split('-'); var resArr = []; dataArr.filter(function(val){ var i = resArr.findIndex(x => x == val); if(i <= -1) resArr.push(val); }); var resData = resArr.join('-'); console.log(resData) 

Split the string on the dashes, create a map keyed by each entry, get an array of all the keys from the map and join them. 在破折号上分割字符串,创建一个由每个条目作为键的地图,从地图中获取所有键的数组并将其连接。

function remove(str) {
  var map = {};
  str.split('-').forEach(
    piece => map[piece] = true
  );
  return Object.keys(map).join('-');
}

We can split the string into an array with the separator being - character and then find the unique elements in the array. 我们可以split字符串split为一个数组,且分隔符为-字符,然后在数组中找到唯一的元素。 Then simply join the array back into string with the delimiter being - 然后,只需join数组回字符串分隔符的存在-

 // Function we'll use to get unique elements Array.prototype.getUnique = function() { var n = []; for (var i = 0; i < this.length; i++) { if (n.indexOf(this[i]) == -1) n.push(this[i]); } return n; } var str = 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc' var arr = str.split('-'); var arrUniq = arr.getUnique(); console.log(arrUniq.join('-')); 

pure javascript ! 纯javascript!

 var str = 'abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc'; str = str.split('-'); uniqueArray = str.filter(function(item, pos) { return str.indexOf(item) == pos; }) str = uniqueArray.join('-'); console.log(str); 

This Regex solves your problem: 此正则Regex解决了您的问题:

/(\b\w+\b)-(?=.*\1)/g

Use like: 使用方式如下:

 function removeDuplicates(str){ return str.replace(/(\\b\\w+\\b)-(?=.*\\1)/g, ''); } removeDuplicates('abc-acb-bac-bca-abc-cab-cab-cba-acb-abc-cab-bca-bca-bac-cba-cab-bca-abc'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM