3

I'm doing this (+ JSON.stringify()) for sending linebreaks via ajax to my mysql server:

var newChatComment = varChatComment.replace(/\r\n|\r|\n/g,'\n');

How can I also replace multiple line breaks with two line break and in case of if the line break(s) is(are) at the end ... remove the line break?

  • plus in case of only one line break ... keep this line break at one?

Edit: The duplicate example pointed to doesn't consider the fact to remove the line break in case the line break is at the end. So this is a new question

Philipp M
  • 3,306
  • 5
  • 36
  • 90
  • In regards to your edit: Removing a linebreak at the end of a string is too trivial to consider this a whole new question. We don't need 26 questions about removing 26 different characters from different positions in a string. – Cerbrus May 11 '17 at 13:46
  • @Cerbrus: This question differs from the one you closed with. I agree with Philipp here. I went through all the answers there. – Wiktor Stribiżew May 11 '17 at 13:54
  • @WiktorStribiżew: then I have [a](http://stackoverflow.com/questions/22962220/remove-multiple-line-breaks-n-in-javascript) [few](http://stackoverflow.com/questions/8253792/replacing-more-than-two-line-breaks-with-regex) [alternative](http://stackoverflow.com/questions/6686433/replace-multiple-n-with-1-in-javascript) dupe targets. Please re-close this as it's an obvious duplicate of _multiple_ questions. – Cerbrus May 11 '17 at 14:05
  • @Cerbrus: The requirements changed 7 minutes ago, see the update. It cannot be a dupe of any of those four. But please do not relieve your downvote. It helps me "keep fit" :) – Wiktor Stribiżew May 11 '17 at 14:10
  • @WiktorStribiżew: I wouldn't consider that change very significant. Besides, it's not like that's never been asked before, either. You assume I cast that downvote, but realistically, you have no idea who downvoted it. – Cerbrus May 11 '17 at 14:12
  • @Cerbrus: Then please find exact dupe that will meet all the OP requirements. And you cannot blame me for answering blatant dupes, I close those evident ones at once. As for downvoting, we've been on SO for a long time, we know how it works :) – Wiktor Stribiżew May 11 '17 at 14:13

1 Answers1

1

If you need to keep a single line break at the start of the string, use

var varChatComment = "\r\n\n1\r\n2\n\n\n3\r\r\n4\r\n\r\n";
var newChatComment = varChatComment.replace(/((?:\r\n?|\n)+)$|(?:\r\n?|\n){2,}/g, function ($0,$1) {
  return $1 ? '' : '\n\n';
});
console.log(newChatComment);

Note that the pattern is built around a (?:\r\n?|\n) construct that matches:

  • \r\n? - a CR and an optional LF
  • | - or
  • \n - an LF symbol.

Details:

  • ((?:\r\n?|\n)+)$ - 1 or more line breaks at the end of the string (those will be removed)
  • | - or
  • (?:\r\n?|\n){2,} - 2 or more line breaks (any style) (this will be turned into 2 newlines.

The ((?:\r\n?|\n)+)$| alternative matches any line break(s) at the end of the string, and if found, these ones are replaced with the empty string.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Note that you can do `" foo ".trimRight()` [if your browser support it.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/TrimRight#Browser_compatibility) – Cerbrus May 11 '17 at 13:49
  • 1
    @Cerbrus: Yeah, if the question was tagged with Chrome/FF or ES6, I'd use an arrow function rather than the anonymous function. – Wiktor Stribiżew May 11 '17 at 13:52
  • @Wiktor. Great thanks. ... your example reduces all number of line breaks to one line break. ... but what I think I need is to reduce it to two line breaks like ... return $1 ? '' : '\n\n'; But in this case ... if there is only one line break ... it also returns two line breaks. How can you exclude that? – Philipp M May 11 '17 at 13:59
  • @PhilippM: Use `/((?:\r\n?|\n)+)$|(?:\r\n?|\n){2,}/g` – Wiktor Stribiżew May 11 '17 at 14:02
  • @Wiktor: That's it, thanks. Is it also cross browser compatible? – Philipp M May 11 '17 at 14:09
  • @PhilippM: Sure, no fancy ES6 features. Fixed the formatting now. – Wiktor Stribiżew May 11 '17 at 14:12