2

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>"
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. Commented Jun 6, 2013 at 23:30
  • question updated, please check it now Commented Jun 7, 2013 at 0:01

3 Answers 3

4

I think the shortest solution is this.

myStr
.split("\n")
.filter((item, i, allItems) => {
  return i === allItems.indexOf(item);
})
.join("\n");
Sign up to request clarification or add additional context in comments.

Comments

1
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.net/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) {

4 Comments

does it check in this order: 1&2, 2&3, 3&4 ?? If so, then how to check 1&3 ?
could not understand your question. please be clearer. my code goes from the fisrt line to the last one, adding each one to the output if it has never appeared or is a <BR>.
sorry I was wrong, just forget. It is working very good but it is not capitalize sensitive. Do you know how to change it?
if (pieces[i].toLowerCase() == '<br>' || pieces[i].toLowerCase() == '<br/>' || pieces[i].toLowerCase() == '<br />' || output.indexOf(pieces[i]) < 0) {
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!

6 Comments

Before using inArray, you could first test for Array.prototype.indexOf, which would likely be much faster if available. Oh, and you only need to check from the current index to the end, not the entire array.
What @RobG said. Mine was a quick once through but he's right, you should add those other things.
You are not taking <br> into account as the OP wants.
Ah, I thought he meant "leave them alone" when he said ignore them. If you want to "ignore" br tags (as in leave them out) then simply add the check to your if (inArray) statement. I'll make the edit now. If you want to remove ALL br tags then that's a little more involved and I think you can figure it out on your own anyways :P
You should also convert your comparison to lower case when performing the <br/> checks because otherwise I think you'll fail checks against upper-case br tags...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.