Using find and replace to delete ALL trailing spaces in regex

I spent weeks trying finding out why my API calls were resulting in error where the quotation mark and comma to end an expression was being pushed to the next line. This was being caused by users entering trailing spaces accidentally into the text fields (possibly after pasting something in).

Support suggested that I use the find and command function to find and replace spaces in the usual way of entering in space/line break with the quotation and comma in the find field then deleting this space leaving just the quotation and comma in the replace field.

The issue I’m facing is that it works well if the user only leaves a single space in the field, because the search and find function assumes only a single space has been left. I’ve been trying various regex expression I Googled so that all trailing spaces, including line breaks should be deleted, but they haven’t seemed to make a difference.

This is what I’ve tried that hasn’t worked for me:

^[ \t]

\s+$
([^\n]*)\s+\r\n

Are you not using :format as JSON safe? That’s what it’s for. You can also use :trimmed to remove leading and trailing whitespace.

2 Likes

That shouldn’t be the case…

Find and replace will find and replace ALL instances of the defined text.

So using find [space] and replace [with nothing] will remove ALL spaces from the text.

If you only want to remove leading and trailing spaces then, as @georgecollier suggests, use :trimmed instead

1 Like

As someone that’s not that good at regex, I’ve found ChatGPT to be really helpful in this regard. Feed it examples of stuff your users are entering, say what you need it to do, and try it out in https://regex101.com/ to see how it does.

I can’t use the JSON-safe function because that switches up the JSON output altogether, but I’ve pre-formatted the JSON in Bubble which works for me.

In terms of :trimmed option, thanks for pointing to this. It looks like a robust way of preventing all sorts of spaces and line breaks. Seems to be working well at the moment in a limited testing environment.

I also realised whilst investigating this issue that Bubble may not display trailing spaces in the Rich Text Editor in the front end reliably. It would show trailing line breaks (or possibly spaces) in the back-end data tab. What ever was in the backend data tab, was in fact the true representation of how the data was structured - and because there were line breaks, it would result in errors - despite first glance from the data in the front end indicating no issue. I sent the issue back to support if they want to use my app to look into it further.

It does, it’s just not very easy to see because whitespace is, well white space, which is shown on a white background. There’s no bug here :slight_smile:

Okay, but the second anyone puts in quotes or maybe backslashes in the input it’ll break.

I tried to use my arrow keys to move the cursor as far as it would go to the right and down as to move onto a new line (if there was a line break there). But it didn’t do either. This is as far the cursor will go right

And this is the data tab - with the cursor sitting at the line break for the same field (I’d expect to see it to the right of the question mark as per the form):

You are right about the JSON-safe expression, and I would like to use it, but my integration is expecting the format to be like this (which is how I written it out in Bubble

With the JSON-safe turned on,

I’m assuming I can remove the quotation marks around the Bubble expression as the output seems to double them up

Yes, just remove the quotation marks and you’re good to go. Format as JSON-safe always puts quotation marks.

1 Like

Thanks, learnt a lot today from this thread!
Now I’m more confident in starting the 200+ fields I need to write out for the rest of my app :sweat_smile: