96

I have a var that contains a big list of words (millions) in this format:

var words =  "
car
house
home
computer
go 
went
";

I want to make a function that will replace the newline between each word with space.

So the results would something look like this:

car house home computer go went

6 Answers 6

178

You can use the .replace() function:

words = words.replace(/\n/g, " ");

Note that you need the g flag on the regular expression to get replace to replace all the newlines with a space rather than just the first one.

Also, note that you have to assign the result of the .replace() to a variable because it returns a new string. It does not modify the existing string. Strings in Javascript are immutable (they aren't directly modified) so any modification operation on a string like .slice(), .concat(), .replace(), etc... returns a new string.

let words = "a\nb\nc\nd\ne";
console.log("Before:");
console.log(words);
words = words.replace(/\n/g, " ");

console.log("After:");
console.log(words);

Sign up to request clarification or add additional context in comments.

Comments

93

In case there are multiple line breaks (newline symbols) and if there can be both \r or \n, and you need to replace all subsequent linebreaks with one space, use

var new_words = words.replace(/[\r\n]+/g," ");

See regex demo

To match all Unicode line break characters and replace/remove them, add \x0B\x0C\u0085\u2028\u2029 to the above regex:

/[\r\n\x0B\x0C\u0085\u2028\u2029]+/g

The /[\r\n\x0B\x0C\u0085\u2028\u2029]+/g means:

  • [ - start of a positive character class matching any single char defined inside it:
    • \r - (\x0D) - \n] - a carriage return (CR)
    • \n - (\x0A) - a line feed character (LF)
    • \x0B - a line tabulation (LT)
    • \x0C - form feed (FF)
    • \u0085 - next line (NEL)
    • \u2028 - line separator (LS)
    • \u2029 - paragraph separator (PS)
  • ] - end of the character class
  • + - a quantifier that makes the regex engine match the previous atom (the character class here) one or more times (consecutive linebreaks are matched)
  • /g - find and replace all occurrences in the provided string.

var words = "car\r\n\r\nhouse\nhome\rcomputer\ngo\n\nwent";
document.body.innerHTML = "<pre>OLD:\n" + words + "</pre>";
var new_words = words.replace(/[\r\n\x0B\x0C\u0085\u2028\u2029]+/g," ");
document.body.innerHTML += "<pre>NEW:\n" + new_words + "</pre>";

Comments

7

Code : (FIXED)

var new_words = words.replace(/\n/g," ");

2 Comments

Did you actually try this? It doesn't seem to work for me: jsfiddle.net/jfriend00/Zn4WE/1
@jfriend00 @Madmartigan TRUE. Sometimes, the obvious is overlooked... (without the /g flag, only the first occurence is replaced)
2

Some simple solution would look like

words.replace(/(\n)/g," ");

3 Comments

Did you actually try this? It doesn't seem to work for me: jsfiddle.net/jfriend00/Zn4WE/1
Why the parens in /(\n)/? They aren't needed.
they are indeed redundant but i prefer to put them to be clearer
2

No need for global regex, use replaceAll instead of replace

myString.replaceAll('\n', ' ')

1 Comment

Upvoted, can u explain why other solution have large space but yours solution have exactly what is necessary?
0

const words = `He had 
concluded that pigs 
must be able 
to fly in Hog Heaven.
`
document.body.innerHTML = "<pre>without-Trim-And-Remove:\n" + words + "</pre>";

trimAndRemoveSymbols=(text)=>{
        return text.replace(/[\n]+/g, '').trim();
    }
document.body.innerHTML += "<pre>Trim-And-Remove:\n" + trimAndRemoveSymbols(words) + "</pre>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.