I have an XML feed that im pulling via javascript and translating it into something more HTML friendly, but im caught up on how to translate carriage returns into an html br tag
I tried something like this
text = text.replace('\r','<br />');
to no avail..
Any ideas?
CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line.
The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.
How Can I Add a New String Line? The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.
Javascript's replace function only replaces the first occurence if you use a string as the matching criteria. To replace all you can use regex.
Try something like this
text = text.replace(/(\r\n|\n|\r)/g,"<br />");
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With