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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With