22

Using this bit of code trims out hidden characters like carriage returns and linefeeds with nothing using javascript just fine:

value = value.replace(/[\r\n]*/g, "");

but when the code actually contains \r\n text what do I do to trim it without affecting r's and n's in my content? I've tried this code:

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

on this bit of text:

{"client":{"werdfasreasfsd":"asdfRasdfas\r\nMCwwDQYJKoZIhvcNAQEBBQADGw......

I end up with this:

{"cliet":{"wedfaseasfsd":"asdfRasdfasMCwwDQYJKoZIhvcNAQEBBQADGw......

Side note: It leaves the upper case versions of R and N alone because I didn't include the /i flag at the end and thats ok in this case.

What do I do to just remove \r\n text found in the string?

Vince
  • 910
  • 2
  • 13
  • 29

3 Answers3

57

If you want to match literal \r and literal \n then you should use the following:

value = value.replace(/(?:\\[rn])+/g, "");

You might think that matching literal \r and \n with [\\r\\n] is the right way to do it and it is a bit confusing but it won't work and here is why:

Remember that in character classes, each single character represents a single letter or symbol, it doesn't represent a sequence of characters, it is just a set of characters.

So the character class [\\r\\n] actually matches the literal characters \, r and n as separate letters and not as sequences.

Edit: If you want to replace all carriage returns \r, newlines \n and also literal \r and '\n` then you could use:

value = value.replace(/(?:\\[rn]|[\r\n]+)+/g, "");

About (?:) it means a non-capturing group, because by default when you put something into a usual group () then it gets captured into a numbered variable that you can use elsewhere inside the regular expression itself, or latter in the matches array.

(?:) prevents capturing the value and causes less overhead than (), for more info see this article.

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
  • Should I also be updating my first bit of code to `value = value.replace(/(\r|\n)+/g, "");` as well? And what is the difference between the () and [] brackets in this case? – Vince Nov 16 '13 at 20:47
  • I've seen the ?: used before but I don't understand what it really does, can you elaborate on it's function? – Vince Nov 16 '13 at 20:48
  • @Vince I have improved the regular expression a bit more, if you have further questions please feel free to ask. – Ibrahim Najjar Nov 16 '13 at 21:04
  • For my own sake I've stuck with your original suggestions till I get a little bit better at this stuff. I've updated my first line of code to `value = value.replace(/(?:\r|\n)+/g, "");` and my second to `value = value.replace(/(?:\\r|\\n)+/g, "");` for now, ty to you and jfriend00 for your suggestions. – Vince Nov 16 '13 at 21:10
  • 1
    The edit for "\r, newlines \n and also literal \r and '\n`" worked very nicely, thanks! – jshaw3 Dec 10 '16 at 02:07
9

To just remove them, this seems to work for me:

value = value.replace(/[\r\n]/g, "");

You don't need the * after the character set because the g flag solves that for you.

Note, this will remove all \r or \n chars whether they are in this exact sequence or not.

Working demo of this option: http://jsfiddle.net/jfriend00/57GtJ/


If you want to remove these characters only when in this exact sequence (e.g. only when a \r is directly followed by a \n, you could use this:

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

Working demo of this option: http://jsfiddle.net/jfriend00/Ta3sn/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
6

If you have text with a lot of \r\n and want to save all of them try this one

value.replace(/(?:\\[rn]|[\r\n])/g,"<br>")

http://jsfiddle.net/57GtJ/63/

Zhurov Konstantin
  • 712
  • 1
  • 8
  • 14