# Using regex\_replace filter in removing empty lines

**URL:** <https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111>\
**Category:** CMS Development\
**Tags:** design-manager, javascript\
**Created:** [May 11, 2021, 3:37pm UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111 "2021-05-11T15:37:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![SR0](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sr0/32/35493_2.png) [@SR0](https://community.hubspot.com/u/SR0)\
**Post date:** [May 11, 2021, 3:37pm UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111/1 "2021-05-11T15:37:19Z")

</div>

I’m currently trying to use regex\_replace filter to remove empty lines.

Below is the script i’m trying to convert to hubl.

```lia-code-sample
var stringWithLineBreaks = `
Lorem ipsum dolor sit amet

- 11.6" HD (1366 x 768) Lorem ipsum

- Consectetur adipiscing elit

- Sed do eiusmod tempor 

- Incididunt ut labore

- Et dolore magna aliqua.

- Consectetur adipiscing elit
`;
var stringWithoutLineBreaks = stringWithLineBreaks.replace(/^\s*[\r\n]/gm,'');//remove empty lines
console.log(stringWithoutLineBreaks);

```

here’s my current hubl filter

`<td>{{item.description|regex_replace("[m=true/^[\t\n\f\r]*[\r\n]/gm]", "")}}</td>`

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [May 12, 2021, 2:29pm UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111/2 "2021-05-12T14:29:49Z")

</div>

@jpsanchez , @Gonzalo somehow I feel like regex is in both of your wheelhouses 😎

---

<div class="post-metadata">

**Author:** ![Gonzalo](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/gonzalo/32/106671_2.png) [@Gonzalo](https://community.hubspot.com/u/Gonzalo)\
**Post date:** [May 12, 2021, 3:37pm UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111/3 "2021-05-12T15:37:41Z")

</div>

I actually know barely nothing about regex, used a few times only Denis xD

Although I would point out a few things:

- regex\_replace filter uses Java RE2 syntax.

- If the string comes from a rich text, Hubspot usually populates empty lines with

_\<p\>\&nbsp;\</p\>_

so you may need to consider the source of it.

Said that, I have found this regex that may work:

`{{ item.description|regex_replace("^[\t\n]*$","") }}`

At least it seems to work in [regex101](https://regex101.com/r/8pOQaB/1)

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [May 12, 2021, 6:57pm UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111/4 "2021-05-12T18:57:04Z")

</div>

For someone who barely knows anything about it, look at you go 😀

---

<div class="post-metadata">

**Author:** ![SR0](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sr0/32/35493_2.png) [@SR0](https://community.hubspot.com/u/SR0)\
**Post date:** [May 12, 2021, 7:08pm UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111/5 "2021-05-12T19:08:12Z")

</div>

Hi @Gonzalo, I tried the regex syntax you suggested. Unfortunately, that too didn’t work.

Thanks

---

<div class="post-metadata">

**Author:** ![jpsanchez](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jpsanchez/32/106486_2.png) [@jpsanchez](https://community.hubspot.com/u/jpsanchez)\
**Post date:** [May 13, 2021, 5:10am UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111/6 "2021-05-13T05:10:14Z")

</div>

Hi,

Not sure if i can help on this 😉 will try my best!

First i will identify txt format is a txt or enrichtxt or HTML?. (the payload) . Second i will try to add (`\l`) to your command line.

Other work arround. I understand you are looking to wrangle as a “make-up”. I suggest you to clean it before, you can do this with the new HubSpot Hub Operations with in a workflow. ( the have some tranformation out-of-the-box new commands) or you can use apps like Trifacta or dataprep to do this.

Hope it helps, not sure about it 😉

Best!

JP

😉

---

<div class="post-metadata">

**Author:** ![SR0](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sr0/32/35493_2.png) [@SR0](https://community.hubspot.com/u/SR0)\
**Post date:** [May 13, 2021, 6:54pm UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111/7 "2021-05-13T18:54:02Z")

</div>

It’s in text format. I added \l but it results to syntax error

`<td>{{item.description|regex_replace("\l[m=true/^[\t\n\f\r]*[\r\n]/gm]", "")}}</td>`

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [May 13, 2021, 3:33pm UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111/8 "2021-05-13T15:33:54Z")

</div>

@Gonzalo Escape “\\” and forgot flags… For multiline it’s ?m: I believe.

@SR0

Regexes provided also won’t work and there’s not a way to definitively tell you which will “work” without clearly stated bounds / expectations. Assuming your text is as copied verbatim - just simple newlines without any “non-printable” characters, isn’t from rich text / contains HTML in the “real” source as Gonzalo noted, and other similar cases a very simple one is just replacing consecutive newlines / escape sequences with the edge case of the last new line.

You could do something like:

regex\_replace(“(?m:^[\\s\\t\\n\\r])+|\\n$”,“”) - The OR operator here just strips last newline as it’s the edge case…

Or, simpler

regex\_replace(“(?m:\\n$)”,“”) if it’s just simple newlines…

Or, normalizing the first… if it has mixed sequences.

regex\_replace(“(?m:[\\s\\t\\r\\n]+$)”,“”)

Or, an infinite other number of regex that are, again, entirely dependent on your use case and boundaries. The missing case in all of these is, again, HTML, hidden character sequences / non-printable ones etc. I’m sure even given regexes could be improved I wasn’t really going for “optimized” here. I was going for “illustrative”, so take them cautiously and I didn’t test these either. So, there’s also that along with the fact I haven’t used regexes in quite some time 😉

Also worth noting that if it’s just simple newlines… You don’t even need a regex to do this if you find them overly complex. Simple string ops suffice.

@dennisedson Still patiently waiting for unicode operations to be unblocked that are accepted, but yet magically don’t work with the filter 🙄

---

<div class="post-metadata">

**Author:** ![SR0](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sr0/32/35493_2.png) [@SR0](https://community.hubspot.com/u/SR0)\
**Post date:** [May 13, 2021, 7:03pm UTC](https://community.hubspot.com/t/using-regex-replace-filter-in-removing-empty-lines/37111/9 "2021-05-13T19:03:50Z")

</div>

Thanks [@Ntbrown](https://community.hubspot.com/t5/user/viewprofilepage/user-id/118753), I used the first one and it worked. It’s think it removed the empty lines, and empty spaces before, after and inbetweent text, whch is what just I needed.

I used js to remove the lines, but I just wanted to maximize the use of Hubl
