我有一个替换数组中的单词的小问题。好的,这是我的数组:
var words = ['Category One Category One Clothes', 'Category One Category One Jackets'];
我想要这样的最终结果:
var result = ['Category One Clothes', 'Category One Jackets'];
尝试使用此方法,但无法使用javascript删除重复的字符串
对于给定的输入,这应该有效。
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);
正则表达式([\w\s]+)
将匹配单词/空格,\1+
并将再次匹配相同的单词。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句