# How to add Line Break after every 64 chars?

**URL:** <https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458>\
**Category:** General\
**Created:** [9 July 2018 09:00 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458 "2018-07-09T09:00:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ram1505](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ram1505/32/958_2.png) [@ram1505](https://discourse.nodered.org/u/ram1505)\
**Post date:** [9 July 2018 09:00 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/1 "2018-07-09T09:00:12Z")

</div>

I've a text file to which I want to add line break after every 64 chars. How can I add this using node-red?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [9 July 2018 09:35 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/2 "2018-07-09T09:35:18Z")

</div>

does the file already contain line breaks or is it one big string?  
do spaces and other special characters count in the 64 characters?

I'd probably use a function node and first get the lenth of the text (using javascript 'length' method) and then have a loop read the text using the javascript 'substring' method to grab 64 characters at a time, add a 'new line' character and stick it into a new string.

Once the loop had processed the input file, I'd exit the loop and write the new string to a new file.

---

<div class="post-metadata">

**Author:** ![ram1505](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ram1505/32/958_2.png) [@ram1505](https://discourse.nodered.org/u/ram1505)\
**Post date:** [9 July 2018 09:48 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/3 "2018-07-09T09:48:10Z")

</div>

It is one very long string. Actually it is a base64 certificate in 1 line. I have to add the line break after every 64 characters to make the certificate a valid file.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [9 July 2018 10:00 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/4 "2018-07-09T10:00:53Z")

</div>

There may be a way to use a regex expression to do it all in one go, but not being a regex expert (it hurts my head) I'd go with the function.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 July 2018 10:06 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/5 "2018-07-09T10:06:04Z")

</div>

Assuming the string is in `msg.payload` then the reg ex would be:

```auto
msg.payload = msg.payload.replace(/(.{64})/g,'$1\n');
return msg;

```

---

<div class="post-metadata">

**Author:** ![ram1505](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ram1505/32/958_2.png) [@ram1505](https://discourse.nodered.org/u/ram1505)\
**Post date:** [9 July 2018 10:14 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/6 "2018-07-09T10:14:39Z")

</div>

Awesome! Works like charm! Thanks @knolleary and @zenofmud

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [9 July 2018 10:16 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/7 "2018-07-09T10:16:01Z")

</div>

See I knew there would be a simple regex way of doing it (but it still hurts my brain 😁)

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [9 July 2018 10:47 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/8 "2018-07-09T10:47:58Z")

</div>

`Replace` also works using a change node and JSONata

`$replace(payload, /(.{64})/, '$1\n')`

But the magic comes from @knolleary 😉

---

<div class="post-metadata">

**Author:** ![nosered1966](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nosered1966/32/52859_2.png) [@nosered1966](https://discourse.nodered.org/u/nosered1966)\
**Post date:** [12 June 2024 05:38 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/9 "2024-06-12T05:38:57Z")

</div>

Good Morning,  
I tried to use your magic code to display a string in a text node on the dashboard and I expected to see a several lines of text, each 10 characters long, but it looks like this:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/71eabe2a762b9504401d65306aeb7049db788495.png)

General question: How can I display "formatted text" (length / line break) on the dashboard.

Thanks  
Thomas

PS:  
I have read the warning about reviving the old post and hope that it is still ok for you to have replied to it.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [12 June 2024 08:40 UTC](https://discourse.nodered.org/t/how-to-add-line-break-after-every-64-chars/1458/10 "2024-06-12T08:40:41Z")

</div>

`<pre>{{msg.payload}}</pre> ?`

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/97d75d10b96c07f2411511c5f3501b0cc5a32ba9.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f3a31d150f95d4d4b4ac598ff951cae90f2c8ce.png)
