Jquery从段落中删除带换行符的行

时间:2015-11-13 13:05:32

标签: jquery replace newline

我需要帮助...我有一个带有编号线的长段落。我需要用它的换行删除一行。

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

上述内容包含在变量str中。我想用jQuery删除第二个条目。 结果就是这样......

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 |亚马逊| HTTPS%3A%2F%2Fwww.amazon.co.in

但是当我使用时:

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

我在下面之间得到一个空白行......

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

我需要删除整行... 怎么做?任何帮助表示赞赏...

2 个答案:

答案 0 :(得分:1)

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", ""));

会这样做..

注意 var x = `...`是一种声明多行字符串的方法。

Play it here

答案 1 :(得分:0)

执行此操作的最佳方法是将线条拆分为数组并删除线条并将其连接回来

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');