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
Post a Comment