简体   繁体   English

如何在 javascript 中用空格替换换行符/换行符?

[英]How can I replace newlines/line breaks with spaces in javascript?

I have a var that contains a big list of words (millions) in this format:我有一个 var,其中包含这种格式的大量单词(数百万):

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

I want to make a function that will replace the newline between each word with space.我想制作一个 function ,它将用空格替换每个单词之间的换行符。

So the results would something look like this:所以结果看起来像这样:

 car house home computer go went

You can use the .replace() function:您可以使用.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.请注意,您需要正则表达式上的g标志来获取 replace 以用空格替换所有换行符,而不仅仅是第一个。

Also, note that you have to assign the result of the .replace() to a variable because it returns a new string.另外,请注意您必须将.replace()的结果分配给一个变量,因为它返回一个新的 string。 It does not modify the existing string.它不会修改现有的 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. Javascript 中的字符串是不可变的(它们不是直接修改的),因此对 string 的任何修改操作(如.slice().concat().replace()等)都会返回一个新的 ZB45CFFE084EDD3D2F2D20。

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

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>";

Code: (FIXED)代码:(固定)

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

Some simple solution would look like一些简单的解决方案看起来像

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

No need for global regex, use replaceAll instead of replace不需要全局正则表达式,使用replaceAll而不是replace

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM