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,229
  • 13
  • 30

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
  • 959,406
  • 173
  • 1,780
  • 1,769
0

use regex:

var str = "First Line" + "\n Second Text Line";
var line = str.replace(/(?:\r|\n).*$/, '');
console.log(line);
Mahendra Kulkarni
  • 1,337
  • 2
  • 23
  • 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,121
  • 11
  • 28