Need to finalize this regex to slugify text

  1. I’m not sure why this isn’t built into Bubble, but…

  2. I have this regex for find/replace to slugify a text string:

find /\W+/g replace with -

This works well. The problem is I need to tweak it to replace multiple hyphens with a single hypen. I tried a simple find “—” replace “-” and that didn’t work.

Thanks in advance.

4 Likes

+1 it’d be great to have a text:formatted as slug function that does this. It’d be ok if it couldn’t check for duplicates etc but for typical cases like replacing space with hyphens & replacing characters like ‘&’ with ‘and’ would make it so much easier

1 Like

Use this as your regEx expression and see if it works
(-)\1+

It should match 2 or more occurrences of the same character, so even if you have 3 hyphens it will still replace it with a single one.

2 Likes

Thanks. That helps clean up my process. In the end I had to generate a formatted text string that I then feed into the Set a thing’s slug workflow action.

Fyi for anyone looking to generate clean slugs, I generated the input text string as follows using chained find-replaces functions:

Replacewith empty = Remove apostrophes
Replace & with and = Convert ampersand
Replace [^0-9a-zA-Z&]+ with dash = convert special chars to dashes
Replace (-)\1+ with dash = replace 2 or more dashes with a single dash
Replace (?<!\S)-|-(?!\S) with empty = trim any leading or trailing dashes

I really do think @Bubble should do this for us as part of its built-in slugify function.

10 Likes

Agreed. See in the thread what I had to figure out and implement. Bubble should be able to do this for us so the skugs they create are always perfect slugs.

Thanks for this!!
I added::
:trimmed
:lowercase
:extract with regex (^[\s\S]{0,250})

The (^[\s\S]{0,250}) gets the first 250 characters
Trimmed might not be needed but I thought what the heck.

Shout out to @seanhoots from his post I searched to get the regex he used for something else here REGEX: remove first 3 characters

1 Like

Good evening,
to solve this situation on bubble and clean up any string to create a perfect slug, I developed a plugin that could help you.
I was myself stuck on a marketplace to succeed in cleaning accents and special characters.
I hope the demo page will help you to understand how to clean your strings efficiently.

3 Likes

HUGE timesaver… huge. I was able to set up a backend workflow and cruise through almost 2500 records and get perfectly formatted slugs for all of them. Thank you! :slight_smile:

It looks like this is built into Bubble. Under workflows “Set a thing’s slug”