1

I have a string I am getting back from an API that looks like this:

[{"lat":"39.142971","lng":"-77.215924","hours":"Monday - Wednesday: 11:30am to 11:00pm\r\nThursday: 11:00am to 11:00pm\r\nFriday-Saturday: 11:00am to 12:00am\r\nSunday: 11:30am to 10:00pm","phone":"3019634847","name":"DFH Alehouse, Gaithersburg"}]

When I parse the hours I am running it through this to make new lines when I display it on a web page:

hours = hours.replace(/(?:\\[rn])+/g, "<br/>");

But when it displays, it is not inserting the line breaks. The odd thing to me is that when I store my own data in a my database and I type it with \n the above code works. But now when I am taking it from the api.

chris85
  • 23,846
  • 7
  • 34
  • 51
Mike
  • 6,751
  • 23
  • 75
  • 132
  • Data is returned from a php script I wrote – Mike Aug 30 '15 at 21:23
  • Don't do this. Instead, format the string using CSS properties such as `white-space`, which will do the right thing. –  Aug 30 '15 at 21:41

2 Answers2

3

Javascript parses \r\n automatic to line breaks. so your regex should look for line breaks (instead of strings). It should look like this:

hours = hours.replace(/\r\n|\n|\r/g,'<br/>');

/\r\n|\n|\r/g This is the "final solution". It first looks for carriage return and linebreak paired, the only line breaks and then carriage returns. (This was inspired by @torazaburo)

As @torazaburo said, you probably shouldn't edit the text, instead you should just add white-space:pre; to your container. This is less resource intensive and uses a more "vanilla" way.

Le 'nton
  • 366
  • 3
  • 22
  • Why do you think JS throws away `\r` for some reason? – zerkms Aug 30 '15 at 21:30
  • Workes like a champ, thanks! – Mike Aug 30 '15 at 21:32
  • This goes back to MS-DOS. to be backward compatible. it has something to do with printers. It is outdated, but good practice. – Le 'nton Aug 30 '15 at 21:32
  • @Le'nton the truth is that `\r` is still there, your solution lefts the redundant `\r` character there in the string. And it's sad OP chosen this solution even though it produces garbage. "It is outdated, but good practice" --- unless it's not. – zerkms Aug 30 '15 at 21:33
  • carriage return is ignored by HTML. but if you are looking to put this into an `pre` node, just extend the regex to `/\r\n/gm` I will put this in the answer. – Le 'nton Aug 30 '15 at 21:38
  • 1
    The `m` flag is irrelevant here. It means something entirely different, namely to interpret anchors (`^` and `$`) in terms of lines within the string. More importantly, you cannot tell what newline convention data coming from the server is going to be following. It might be `\r\n`, it might be `\r`, or it might be `\n`. A correct solution must account for all of these. –  Aug 30 '15 at 21:43
  • @torazaburo is it okay like this? – Le 'nton Aug 30 '15 at 22:04
  • @Le'nton Looks fine, thanks. Except normally `pre-wrap` or `pre-line` works better as a value for `white-space`. –  Aug 31 '15 at 03:34
  • @torazaburo but in this case it's not relevant. It only matters when there are leading whitespaces. Right? – Le 'nton Aug 31 '15 at 09:41
  • No, it handles runs of whitespace differently. `pre` will keep groups of spaces, whereas `pre-wrap` etc. will condense them down to one as normal. –  Aug 31 '15 at 11:21
0

Your regular expression tries to find a backslash then 'r' or 'n'.

Try instead:

/[\n\r]{0,2}/g

which find all newlines or carriage-return

AdminXVII
  • 1,319
  • 11
  • 22