Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace '\n' in javascript [duplicate]

I'm trying to do replace in JavaScript using:

r = "I\nam\nhere";
s = r.replace("\n"," ");

But instead of giving me

I am here

as the value of s, It returns the same.

Where's the problem??

like image 552
Moin Avatar asked Dec 28 '12 16:12

Moin


People also ask

Can we use \n in JavaScript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

What does \r mean JavaScript?

The RegExp \r Metacharacter in JavaScript is used to find the carriage return character (Carriage return means to return to the beginning of the current line without advancing downward). If it is found it returns the position else it returns -1. Syntax: /\r/

What is replace () in JavaScript?

The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.

What can I use instead of replaceAll in JavaScript?

To make the method replace() replace all occurrences of the pattern you have to enable the global flag on the regular expression: Append g after at the end of regular expression literal: /search/g. Or when using a regular expression constructor, add 'g' to the second argument: new RegExp('search', 'g')


9 Answers

As stated by the others the global flag is missing for your regular expression. The correct expression should be some thing like what the others gave you.

var r = "I\nam\nhere";
var s = r.replace(/\n/g,' ');

I would like to point out the difference from what was going on from the start. you were using the following statements

var r = "I\nam\nhere";
var s = r.replace("\n"," ");

The statements are indeed correct and will replace one instance of the character \n. It uses a different algorithm. When giving a String to replace it will look for the first occurrence and simply replace it with the string given as second argument. When using regular expressions we are not just looking for the character to match we can write complicated matching syntax and if a match or several are found then it will be replaced. More on regular expressions for JavaScript can be found here w3schools.

For instance the method you made could be made more general to parse input from several different types of files. Due to differences in Operating system it is quite common to have files with \n or \r where a new line is required. To be able to handle both your code could be rewritten using some features of regular expressions.

var r = "I\ram\nhere";
var s = r.replace(/[\n\r]/g,' ');
like image 76
Pablo Jomer Avatar answered Oct 16 '22 14:10

Pablo Jomer


use s = r.replace(/\\n/g," ");

Get a reference:

The "g" in the javascript replace code stands for "greedy" which means the replacement should happen more than once if possible

like image 44
Grijesh Chauhan Avatar answered Oct 16 '22 16:10

Grijesh Chauhan


The problem is that you need to use the g flag to replace all matches, as, by default, replace() only acts on the first match it finds:

var r = "I\nam\nhere",
    s = r.replace(/\n/g,' ');

To use the g flag, though, you'll have to use the regular expression approach.

Incidentally, when declaring variables please use var, otherwise the variables you create are all global, which can lead to problems later on.

like image 20
David Thomas Avatar answered Oct 16 '22 16:10

David Thomas


.replace() needs the global match flag:

s = r.replace(/\n/g, " ");
like image 35
CassOnMars Avatar answered Oct 16 '22 14:10

CassOnMars


It's working for me:

var s = r.split('\\n').join(' ');
like image 24
Ivan Kalashnik Avatar answered Oct 16 '22 16:10

Ivan Kalashnik


You can use:

var s = r.replace(/\n/g,' ').replace(/\r/g,' ');

because diferents SO use diferents ways to set a "new line", for example: Mac Unix Windows, after this, you can use other function to normalize white spaces.

like image 23
Daniel Antonio Nuñez Carhuayo Avatar answered Oct 16 '22 16:10

Daniel Antonio Nuñez Carhuayo


Just use \\\n to replace it will work.

r.replace("\\\n"," ");
like image 24
goshant meher Avatar answered Oct 16 '22 14:10

goshant meher


The solution from here worked perfect for me:

r.replace(/=(\r\n|\n|\r)/gm," ");
like image 20
hestellezg Avatar answered Oct 16 '22 16:10

hestellezg


replaceAll() is relative new, not supported in all browsers:

r = "I\nam\nhere";
s = r.replaceAll("\n"," ");
like image 28
OhadR Avatar answered Oct 16 '22 15:10

OhadR