9

I am trying to find and replace line breaks in text using javascript.

The following works. It replaces the character a with the character z.

var text = "aasdfasdf";
text= text.replace(/a/g,"z");
alert(text);

The following based on other posts on this and other message boards does not. Basically the javascript does not fire:

var text = "aasdfasdf";
text= text.replace(/\n/g,"z");
alert(text);

...Here is one of many posts that suggests it should work.

JavaScript: How to add line breaks to an HTML textarea?

and by the way following does not work for me in Firefox either:

text = text.replace(/\n\r?/g, '<br />'); or
text = text.replace("\n", '<br />'); 

Note: I realize there are no line breaks in the text variable..I am just using a simple string for testing purposes.

Can anyone see what could be wrong or show me a way to do this that actually works.

Thanks.

Community
  • 1
  • 1
user1260310
  • 2,229
  • 9
  • 49
  • 67
  • `"hello\nworld".replace(/\r?\n/g, ' ') === 'hello world'` ... I don't see the problem. – Ja͢ck Nov 23 '12 at 16:36
  • The escaped character ie \ seems to be breaking it. Very frustrating. – user1260310 Nov 23 '12 at 16:38
  • Possible duplicate of [How do I replace all line breaks in a string with
    tags?](https://stackoverflow.com/questions/784539/how-do-i-replace-all-line-breaks-in-a-string-with-br-tags)
    – Jun Oct 26 '18 at 00:30

4 Answers4

14

I'd cover my bets by handling \r\n (the sequence), and then handling \r and \n through a character class, like this:

text = text.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');

The first replace turns the sequence \r\n into <br />. The second replace replaces any \r or \n characters found on their own with the string.

More on regular expressions in JavaScript here.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • On Windows, line breaks are `\r\n` and with your regex, you will obtain `

    `...
    – Samuel Caillerie Nov 23 '12 at 16:34
  • This script did not fire for me... Something is breaking code. – user1260310 Nov 23 '12 at 16:35
  • @SamuelCaillerie: Actually, on Windows, whether the line breaks end up being `\r\n` or `\n` depends on what browser you use. (Strange, but true.) But you make a good point, on at least some browsers they'll still be `\r\n`. I've updated. – T.J. Crowder Nov 23 '12 at 16:41
  • @user1260310: You'll have to provide an example of the problem, then, and a more descriptive error than "did not fire" whatever that means. – T.J. Crowder Nov 23 '12 at 16:41
  • Well it works okay in this jsfiddle. http://jsfiddle.net/qCt88/ There must be something else wrong in my code but I cannot figure it out. Can you suggest a way to trap and report errors in javascript. I usually use alerts to see what is going on...when I say did not fire, I mean no alert – user1260310 Nov 23 '12 at 16:43
  • @user1260310: *"Can you suggest a way to trap and report errors in javascript."* Yes: Use a debugger. There's a full-featured debugger built into nearly all modern browsers. In many of them, F12 opens the debugger. If not, you can find it on the menus, usually under "developer tools" or similar. The debugger in Chrome and Opera is excellent. The built-in one in Firefox isn't brilliant, but you can add Firebug (an add-on) which is pretty good although I've routinely had stability probs with it. IE8 and above have "F12 Developer Tools" which are vaguely functional in IE8 and a lot better in IE9. – T.J. Crowder Nov 23 '12 at 16:45
  • Debugger in firefox does not pull up script. freezes on loading.... However, Problem seems to be in link. See jsfiddle. http://jsfiddle.net/qCt88/6/ – user1260310 Nov 23 '12 at 19:41
  • Marked this right cause I think it probably is right in most cases. It is not working for my method of calling javascript, however: Show Text. See jsfiddle http://jsfiddle.net/qCt88/6/ – user1260310 Nov 24 '12 at 00:23
  • @user1260310: That fiddle basically has nothing in it. The reason it *does* nothing is that you've told jsFiddle to put your code in `onload` (this is jsFiddle's default), and so your `showText` function isn't global. Since it's not global, you can't call it from a DOM0 handler. Here's your fiddle corrected, with useful test data, and incorporating the above: http://jsfiddle.net/qCt88/11/ – T.J. Crowder Nov 24 '12 at 07:47
2

To handle windows new line characters try

text = text.replace(/\r\n/g, '<br />').replace(/[\r\n]/g, '<br />');
Bruno
  • 5,772
  • 1
  • 26
  • 43
1

ECMAScript normalizes line breaks in strings to "\n\r" and the DOM normalizes line breaks in strings to "\n". Both of those OS agnostic which these formats:

  • Windows - CRLF
  • Unix - LF
  • Old Mac - CR

The right way to accomplish this task depends on how you are receiving the string and how you are writing it out.

austincheney
  • 1,189
  • 9
  • 11
1

Another solution for both types of line endings

str.replace(new RegExp('\r?\n','g'), '<br />');
WSkinner
  • 2,147
  • 1
  • 19
  • 21