最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to delete duplicate lines from string? - Stack Overflow

programmeradmin1浏览0评论

How to write a script in javascript which check all lines ("Line1\nLine2\nLine3...") in a string and if there are duplicate lines then just leave one and ignore br tags?

var s = "Hello world\n<BR>\nThis is some text\nThis is some text\n<BR>\nThis is some text"
line1 = "Hello world"
line2 = "<BR>"
line3 = "This is some text"
line4 = "This is some text"
line5 = "<BR>"
line6 = "This is some text"

var result = "Hello world\n<BR>\nThis is some text\n<BR>"
line 1 = "Hello world"
line 2 = "<BR>"
line 3 = "This is some text"
line 4 = "<BR>"

How to write a script in javascript which check all lines ("Line1\nLine2\nLine3...") in a string and if there are duplicate lines then just leave one and ignore br tags?

var s = "Hello world\n<BR>\nThis is some text\nThis is some text\n<BR>\nThis is some text"
line1 = "Hello world"
line2 = "<BR>"
line3 = "This is some text"
line4 = "This is some text"
line5 = "<BR>"
line6 = "This is some text"

var result = "Hello world\n<BR>\nThis is some text\n<BR>"
line 1 = "Hello world"
line 2 = "<BR>"
line 3 = "This is some text"
line 4 = "<BR>"
Share Improve this question edited Sep 2, 2014 at 12:15 Unihedron 11k13 gold badges63 silver badges72 bronze badges asked Jun 6, 2013 at 23:21 maniootekmaniootek 4191 gold badge8 silver badges15 bronze badges 2
  • split on \n, loop through the array, ignore <br>, set to a hash, if it is in hash, than remove, after loop join together. – epascarello Commented Jun 6, 2013 at 23:30
  • question updated, please check it now – maniootek Commented Jun 7, 2013 at 0:01
Add a ment  | 

3 Answers 3

Reset to default 4

I think the shortest solution is this.

myStr
.split("\n")
.filter((item, i, allItems) => {
  return i === allItems.indexOf(item);
})
.join("\n");
var pieces = s.split("\n"); //This will split your string
var output = []; //Output array

for (var i = 0; i < pieces.length; i++) { //Iterate over input...

   if (pieces[i] == '<BR>' || output.indexOf(pieces[i]) < 0) { //If it is <BR> or not in output, add to output
      output.push(pieces[i]);
   }

}

var newS = output.join("\n"); //Concatenates the string back, you don't have to do this if you want your lines in the array

Here we have the jsFiddle: http://jsfiddle/7s88t/

For you knowledge, the indexOf function returns the position where pieces[i] is at output array. If it is not found, it returns -1. That is why I check if it is less than zero.

Hope I have helped.

EDIT

As you requested, to take lower case:

if (pieces[i].toLowerCase() == '<br>' || pieces[i].toLowerCase() == '<br/>' || pieces[i].toLowerCase() == '<br />' || output.indexOf(pieces[i]) < 0) {

1) divide your text into an array by line break:

var arr = s.split("\n");

2) Remove all duplicate entries:

var str;
for(var i=0; i<arr.length; i++) {
    str = arr[i];
    //Takes into account all bad forms of BR tags. Note that in your code you are using
    //invalid br tags- they need to be <br /> (self-closing)
    if(inArray(str, arr) && str != "<br>" && str != "<br/>" && str != "<br />"){
        arr.splice(i, 1);
    }
};

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

3) Make them back into a string

//Note joining with newline characters will preserve your line outputs :)
var output = arr.join("\n"); 

This approach is good because it avoids using regex, doesn't even need to consider <br /> tags, and uses native JS meaning you can put it anywhere you want. I didn't test this code, I just wrote it out so it may contain errors. But it should be a good starting point. Cheers!

发布评论

评论列表(0)

  1. 暂无评论