fran35 fran35 - 1 year ago 86
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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download