0

I have manage to detect a line break in a string, but have can i remove it properly so in string would be only left "First Line"

var str = "First Line"         
+"\nSecond Text Line";

var res = /\r|\n/.exec(str);
console.log(res);
Andrew
  • 1,507
  • 1
  • 22
  • 42

3 Answers3

4

There are several ways, but if you may have \r or \n, I'd probably use replace with a regex:

str = str.replace(/(?:\r|\n).*$/, '');
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

use regex:

var str = "First Line" + "\n Second Text Line";
var line = str.replace(/(?:\r|\n).*$/, '');
console.log(line);
Mahendra Kulkarni
  • 1,437
  • 2
  • 26
  • 35
-1

split string using a "\n" as a delimeter and then join the string back together

var strings = str.split("\n");
var finalstr =strings[0];
henrybbosa
  • 1,139
  • 13
  • 28