var words = ['world','hel','wor','hello']; str = words.sort().join(' ').replace(/(\S+)(.*?)(\1\S+)/g,"$2$3").match(/\S+/g) alert(str)