Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does replace(/\n/g, "something") mean?

Tags:

javascript

I've come across this code many times and have never figured out what it means

/\n/g
like image 640
SummerBoy Avatar asked Aug 19 '16 04:08

SummerBoy


1 Answers

This will replace all new line character in your string with "something" . i.e

var str="Hi\nHello";
console.log(str);

Output:

Hi

Hello

console.log(str.replace(/\n/g,"and"));

Output:

HiandHello

console.log(str.replace(/\n/g,""));

Output:

HiHello

like image 68
Ruhul Avatar answered Oct 05 '22 18:10

Ruhul