Javascript / jQuery在数组的其余部分中查找并删除数组值

好吧,不知道如何提出这个问题,但我正在尝试将值集合到一个数组中,但每个值可能在其余值上有重复(或附加)字符串,我相信它们总是建立在每个值之上其他…

这就是我的意思。

如果我要输出一个数组,它将是这样的:

[0] => This is a string. [1] => here's another string. This is a string. [2] => And now there's a third string. here's another string. This is a string. [3] => Here's a string that might not follow the pattern. This is a string. 

我想最终输出的是

 [0] => This is a string. [1] => here's another string. [2] => And now there's a third string. [3] => Here's a string that might not follow the pattern. 

基本上,任何字符串都不能在任何其他字符串中包含重复的文本。

不确定我是否有意义。

编辑:

这是完整的故事 – 我正在通过PHP IMAP使用AJAX脚本收集电子邮件。 我从每封电子邮件中获取了正文,但不幸的是,没有办法通过所有电子邮件服务找到引用的文本。 所以我所做的就是让每个电子邮件正文显示,我已经摆脱了任何额外的字符(如行引号>>字符)。

我现在要做的是:从第一封电子邮件开始,检查其他电子邮件,并删除第一个字符串(如果它在那里),然后使用第二封电子邮件,检查其余电子邮件并删除该字符串,依此类推。

它并不完美,但它会缩短很多信息。

在使用空字符串首次替换新数组中的每个字符串之后,这将使用旧数组中的每个字符串构建一个新数组。

 var arr = [ 'This is a string.', "here's another string. This is a string.", "And now there's a third string. here's another string. This is a string.", "Here's a string that might not follow the pattern. This is a string." ]; var out = arr.reduce(function(w, s) { w.push(w.reduce(function(s1, e) { return s1.replace(e, ""); }, s).trim()); return w; }, []); alert(out); 
 var strings = [ "This is a string.", "here's another string. This is a string.", "And now there's a third string. here's another string. This is a string.", "Here's a string that might not follow the pattern. This is a string." ]; var i, j, pos; for (i = 0; i < strings.length; i += 1) { for (j = 0; j < strings.length; j += 1) { if (i === j) { continue; } pos = strings[i].indexOf(strings[j]); if (pos !== -1) { strings[i] = strings[i].substr(0, pos) + strings[i].substr(pos + strings[j].length); } } } console.log(strings); output: [ "This is a string.", "here's another string. ", "And now there's a third string. ", "Here's a string that might not follow the pattern. " ]; 

请注意,此代码不会替换多次出现。 如果你想要,添加一个while循环。

编辑:如果字符串具有特殊的正则表达式字符,这将导致问题。 没有正则表达式的JS全局repalce可以利用splitjoin ,( http://www.adequatelygood.com/JS-Find-and-Replace-with-SplitJoin.html )见下文:

Javascript需要正则表达式来进行全局替换,所以像这样

 var arr = [ 'This is a string.', "here's another string. This is a string.", "And now there's a third string. here's another string. This is a string.", "Here's a string that might not follow the pattern. This is a string." ]; for (var i = 0; i < arr.length; i++) { for (j = 0; j < arr.length; j++) { if (j !== i) { arr[i] = arr[i].split(arr[j]).join(""); } } } console.log(arr); [ "This is a string.", "here's another string.", "And now there's a third string.", "Here's a string that might not follow the pattern." ] 

它应该取代所有出现

使用如下。 使用jquery的.map函数迭代数组。 代码如下

 var arr = [ 'This is a string.', "here's another string. This is a string.", "And now there's a third string. here's another string. This is a string.", "Here's a string that might not follow the pattern. This is a string." ]; var out=$.map(arr,function (i,v){ return i.substr(0,i.indexOf(".") + 1); }); console.log("value is"+out);