Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicate words in array elements

I have a small problem with replace words in array. Ok, this my array:

var words = ['Category One Category One Clothes', 'Category One Category One Jackets'];

I want final result like this:

var result = ['Category One Clothes', 'Category One Jackets'];

Try with this method, but not working Removing duplicate strings using javascript

like image 364
Opsional Avatar asked Sep 23 '26 11:09

Opsional


1 Answers

For the given input, this should work.

words.map(w => w.replace(/([\w\s]+)\1+/, '$1'))

var words = ['Category One Category One Clothes', 'Category One Category One Jackets'];
var result = words.map(w => w.replace(/([\w\s]+)\1+/, '$1'));
console.log(result);

The regex ([\w\s]+) will match the words/spaces and \1+ will match the same word again.

like image 141
Tushar Avatar answered Sep 26 '26 02:09

Tushar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!