Ninoop p george
Ninoop p george

Reputation: 678

Jquery remove a line with line break from a paragraph

I need help on this... I have a long paragraph with numbered lines. I need to remove a line with it's linebreak.

E.g

1|ncubicx|http%3A%2F%2Fwww.ncubicx.com
2|asd|fdf
3|asd|asd
4|as|asd
5|sds|sds
6|asa|asa
7|asd|fdf
8|google|https%3A%2F%2Fwww.google.com
9|flipkart|http%3A%2F%2Fwww.flipkart.com%2F
10|amazon|https%3A%2F%2Fwww.amazon.co.in

The above one is contained in a variable str. I want to remove the 2nd entry with jQuery. So that the result becomes like this...

1|ncubicx|http%3A%2F%2Fwww.ncubicx.com
3|asd|asd
4|as|asd
5|sds|sds
6|asa|asa
7|asd|fdf
8|google|https%3A%2F%2Fwww.google.com
9|flipkart|http%3A%2F%2Fwww.flipkart.com%2F

10|amazon|https%3A%2F%2Fwww.amazon.co.in

But when I use:

oldrep="2|asd|fdf";    
var res = str.replace(oldrep, "");

I get a blank line in between like below...

1|ncubicx|http%3A%2F%2Fwww.ncubicx.com

3|asd|asd
4|as|asd
5|sds|sds
6|asa|asa
7|asd|fdf
8|google|https%3A%2F%2Fwww.google.com
9|flipkart|http%3A%2F%2Fwww.flipkart.com%2F
10|amazon|https%3A%2F%2Fwww.amazon.co.in

I need to remove the whole line... How to do it? Any help is appreciated...

Upvotes: 1

Views: 276

Answers (2)

Kishore Sahasranaman
Kishore Sahasranaman

Reputation: 4230

Best way to do this is to split the lines to a array and remove the line and join it back

var str = `1|ncubicx|http%3A%2F%2Fwww.ncubicx.com
2|asd|fdf
3|asd|asd
4|as|asd
5|sds|sds
6|asa|asa
7|asd|fdf
8|google|https%3A%2F%2Fwww.google.com
9|flipkart|http%3A%2F%2Fwww.flipkart.com%2F
10|amazon|https%3A%2F%2Fwww.amazon.co.in`;

 var strArray = str.split('\n');
 strArray.splice(1, 1);
 var newStr = strArray.join('\n');

Upvotes: 0

void
void

Reputation: 36703

var x = `1|ncubicx|http%3A%2F%2Fwww.ncubicx.com
2|asd|fdf
3|asd|asd
4|as|asd
5|sds|sds
6|asa|asa
7|asd|fdf
8|google|https%3A%2F%2Fwww.google.com
9|flipkart|http%3A%2F%2Fwww.flipkart.com%2F
10|amazon|https%3A%2F%2Fwww.amazon.co.in`;

console.log(x.replace("2|asd|fdf\n", ""));

Will do it..

Note var x = `...` is a way of declaring a multiline string.

Play it here

Upvotes: 1

Related Questions