How to remove all line breaks from a string

前端 未结 16 1252
轮回少年
轮回少年 2020-11-22 11:36

I have a text in a textarea and I read it out using the .value attribute.

Now I would like to remove all linebreaks (the character that is produced when you press

相关标签:
16条回答
  • 2020-11-22 12:12

    The answer provided by PointedEars is everything most of us need. But by following Mathias Bynens's answer, I went on a Wikipedia trip and found this: https://en.wikipedia.org/wiki/Newline.

    The following is a drop-in function that implements everything the above Wiki page considers "new line" at the time of this answer.

    If something doesn't fit your case, just remove it. Also, if you're looking for performance this might not be it, but for a quick tool that does the job in any case, this should be useful.

    // replaces all "new line" characters contained in `someString` with the given `replacementString`
    const replaceNewLineChars = ((someString, replacementString = ``) => { // defaults to just removing
      const LF = `\u{000a}`; // Line Feed (\n)
      const VT = `\u{000b}`; // Vertical Tab
      const FF = `\u{000c}`; // Form Feed
      const CR = `\u{000d}`; // Carriage Return (\r)
      const CRLF = `${CR}${LF}`; // (\r\n)
      const NEL = `\u{0085}`; // Next Line
      const LS = `\u{2028}`; // Line Separator
      const PS = `\u{2029}`; // Paragraph Separator
      const lineTerminators = [LF, VT, FF, CR, CRLF, NEL, LS, PS]; // all Unicode `lineTerminators`
      let finalString = someString.normalize(`NFD`); // better safe than sorry? Or is it?
      for (let lineTerminator of lineTerminators) {
        if (finalString.includes(lineTerminator)) { // check if the string contains the current `lineTerminator`
          let regex = new RegExp(lineTerminator.normalize(`NFD`), `gu`); // create the `regex` for the current `lineTerminator`
          finalString = finalString.replace(regex, replacementString); // perform the replacement
        };
      };
      return finalString.normalize(`NFC`); // return the `finalString` (without any Unicode `lineTerminators`)
    });
    
    0 讨论(0)
  • 2020-11-22 12:13

    How you'd find a line break varies between operating system encodings. Windows would be \r\n, but Linux just uses \n and Apple uses \r.

    I found this in JavaScript line breaks:

    someText = someText.replace(/(\r\n|\n|\r)/gm, "");
    

    That should remove all kinds of line breaks.

    0 讨论(0)
  • 2020-11-22 12:14

    This is probably a FAQ. Anyhow, line breaks (better: newlines) can be one of Carriage Return (CR, \r, on older Macs), Line Feed (LF, \n, on Unices incl. Linux) or CR followed by LF (\r\n, on WinDOS). (Contrary to another answer, this has nothing to do with character encoding.)

    Therefore, the most efficient RegExp literal to match all variants is

    /\r?\n|\r/
    

    If you want to match all newlines in a string, use a global match,

    /\r?\n|\r/g
    

    respectively. Then proceed with the replace method as suggested in several other answers. (Probably you do not want to remove the newlines, but replace them with other whitespace, for example the space character, so that words remain intact.)

    0 讨论(0)
  • 2020-11-22 12:15

    USE THIS FUNCTION BELOW AND MAKE YOUR LIFE EASY

    The easiest approach is using regular expressions to detect and replace newlines in the string. In this case, we use replace function along with string to replace with, which in our case is an empty string.

    function remove_linebreaks( var message ) {
        return message.replace( /[\r\n]+/gm, "" );
    }
    

    In the above expression, g and m are for global and multiline flags

    0 讨论(0)
  • If it happens that you don't need this htm characte &nbsp shile using str.replace(/(\r\n|\n|\r)/gm, "") you can use this str.split('\n').join('');

    cheers

    0 讨论(0)
  • 2020-11-22 12:17

    This will replace the line break by empty space.

    someText = someText.replace(/(\r\n|\n|\r)/gm,"");
    

    Read more on this article.

    0 讨论(0)
提交回复
热议问题