Rahul Mankar Rahul Mankar - 2 months ago 8x
Node.js Question

How do I replace multiple words from string in nodejs?

I would like to replace multiple words from below string:

\njava developer\n

How do I replace \n from start and \n from last from above string?
I used

replace('\n', '')

but it replace first \n only.


If you want to replace all \n without calling replace in a loop you have to use a regular expression. You can use it like this:

var test = "\njava developer\n";
var result = test.replace(/\n/g, '');

the g in the regular expression means replace all occurrences.

Hope this helps.