javascript - Change \n or \r into <br/> -


this question has answer here:

i have string getting api looks this:

[{"lat":"39.142971","lng":"-77.215924","hours":"monday - wednesday: 11:30am 11:00pm\r\nthursday: 11:00am 11:00pm\r\nfriday-saturday: 11:00am 12:00am\r\nsunday: 11:30am 10:00pm","phone":"3019634847","name":"dfh alehouse, gaithersburg"}] 

when parse hours running through make new lines when display on web page:

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

but when displays, not inserting line breaks. odd thing me when store own data in database , type \n above code works. when taking api.

javascript parses \r\n automatic line breaks. regex should line breaks (instead of strings). should this:

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

/\r\n|\n|\r/g "final solution". first looks carriage return , linebreak paired, line breaks , carriage returns. (this inspired @torazaburo)

as @torazaburo said, shouldn't edit text, instead should add white-space:pre; container. less resource intensive , uses more "vanilla" way.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -