Вопрос по javascript, regex, string – Как удалить все разрывы строк из строки

322

У меня есть текст в текстовой области, и я прочитал его, используя атрибут .value.

Теперь я хотел бы удалить все разрывы строк (символ, который появляется при нажатииEnter) из моего текста теперь используется .replace с регулярным выражением, но как мне указать разрыв строки в регулярном выражении?

Если это невозможно, есть ли другой путь?

Error: User Rate Limit ExceededHow do I replace all line breaks in a string with <br /> tags? Jun711

Ваш Ответ

11   ответов
14
var str = "bar\r\nbaz\nfoo";

str.replace(/[\r\n]/g, '');

>> "barbazfoo"
375

Error: User Rate Limit Exceeded\rError: User Rate Limit Exceeded\nError: User Rate Limit Exceeded\r\nError: User Rate Limit Exceedednothing делать с кодировкой символов.)

Error: User Rate Limit ExceededRegExp буквальный, чтобы соответствовать всем вариантам

/\r?\n|\r/

Error: User Rate Limit Exceeded

/\r?\n|\r/g

Error: User Rate Limit ExceededreplaceError: User Rate Limit ExceedednotError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit ExceedednotError: User Rate Limit Exceeded
Error: User Rate Limit ExceedednotError: User Rate Limit Exceededw3.org/TR/html4/struct/text.html#whitespace
Error: User Rate Limit Exceeded.valueError: User Rate Limit Exceededtextarea.value = 'a\u2029b'; textarea.value.charAt(1) == '\u2029'; // trueError: User Rate Limit Exceeded
21

Error: User Rate Limit Exceeded

myString.replace(/[^\x20-\x7E]/gmi, "")

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededNOTError: User Rate Limit Exceeded0x20-0x7EError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
34

\n\r

var str2 = str.replace(/\n|\r/g, "");

\n\r

Error: User Rate Limit Exceeded
/\n|\r/gError: User Rate Limit Exceeded/[\n\r]/gError: User Rate Limit Exceeded/[\n\r]+/gError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
5

Error: User Rate Limit Exceeded

var test = 'this\nis\na\ntest\nwith\newlines';
console.log(test.replace(/\n/g, ' '));
12

Error: User Rate Limit Exceeded

let str = '\t\n\r this  \n \t   \r  is \r a   \n test \t  \r \n';
str.replace(/\s+/g, ' ').trim();
console.log(str); // logs: "this is a test"

.replace()Error: User Rate Limit Exceeded/\s+/gError: User Rate Limit Exceededall groupsError: User Rate Limit Exceeded.trim()Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
[ \f\n\r\t\v​\u00a0\u1680​\u2000​-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]

86

String.trim()Error: User Rate Limit Exceeded

   \n \n\n Hey! \n I'm a string!!!         \n\n";
const trimmedString = myString.trim();

console.log(trimmedString);
// outputs: "Hey! \n I'm a string!!!"

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

NOTE!Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
5

Error: User Rate Limit ExceededError: User Rate Limit Exceeded.

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

// 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`)
});
Error: User Rate Limit Exceededreplace()Error: User Rate Limit ExceededlineTerminatorsError: User Rate Limit Exceeded
2

var break_for_winDOS = 'test\r\nwith\r\nline\r\nbreaks';
var break_for_linux = 'test\nwith\nline\nbreaks';
var break_for_older_mac = 'test\rwith\rline\rbreaks';

break_for_winDOS.replace(/(\r?\n|\r)/gm, ' ');
//output
'test with line breaks'

break_for_linux.replace(/(\r?\n|\r)/gm, ' ');
//output
'test with line breaks'

break_for_older_mac.replace(/(\r?\n|\r)/gm, ' ');
// Output
'test with line breaks'
390

\r\n\n и Apple использует\r.

:

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

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded\r\n and \n and \rError: User Rate Limit Exceeded/[\n\r]/gError: User Rate Limit Exceeded
12

Error: User Rate Limit Exceeded

yourString.replace(/\r?\n?/g, '')

Error: User Rate Limit Exceeded

yourString.trim()

Похожие вопросы