fran35 fran35 - 10 months ago 46
Javascript Question

How to trim text after special character \n in js

In the following text:


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

how can I do it?

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

Answer Source

\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)