fran35 fran35 - 1 month ago 7
Javascript Question

How to trim text after special character \n in js

In the following text:

text\n\nWhy';


I'm trying to get rid of everything after \ (sign included) so that my output will be:
text

how can I do it?
Tried:

var s = 'text\n\nWhy';
s = s.substring(0, s.indexOf('\'));
console.log (s)

Answer

\n is an escape character for newline. You will need to search for that "character".

var s = 'text\n\nWhy';
s = s.substring(0, s.indexOf('\n'));
console.log (s)