Using regex replace to shorten long messages

Put simply, I want to shorten text from a FB post to a limit of 500 characters and if it is shortened, add “ …” at the end. Yet for some reason this isn’t matching/replacing despite working in regex testers. There is no error and the full string is returned.

replace(7.message; “/(?<=.{500}) .+/gs”; " …")

Welcome to the Make community!

You should simply use the slice function and then manually append the ellipsis at the end.

Hope this helps! Let me know if there are any further questions or issues.

@samliew


P.S.: Did you know, the concepts of about 70% of questions asked on this forum are already covered in the Make Academy. Investing some effort into it will save you lots of time and frustration using Make later!

That won’t work, because it will then ALWAYS add the ellipsis even if it wasn’t truncated.

The issue with this is Make’s replace function only accepts one regex flag at a time, and you’re using two.

I’ve already reported this bug a year ago.

You can use the if function to check the length of the string, and then truncate if necessary.

Ah! I am glad to know WHY it fails.

Do you have an example of using the if function in this way? I originally turned to regex replace after some other thread stating it wasn’t possible any other way, but that was years ago.

EDIT: The other reason for using regex was to not split it in the middle of a word…

Okay so I used substring temporarily just to get past a really long post that was holding up my queue, then tried some other things.

  1. I can’t get if() to work right. I tried checking if length(7.message)>500 but it seems to ALWAYS evaluate to true. So it always adds ellipsis to the message.

  2. I figured I would test your theory about multiple regex flags and I switched it from /gs to just /s but it still didn’t replace anything.

It has already been answered before, see

If you struggle with functions, try using a router and filters instead.

If you need further assistance, please provide the following:

1. Relevant Screenshots

Please share screenshots of your scenario, any error messages, relevant module fields, and filters in question? It would really help other community members to see what you’re looking at.

You can upload images here using the Upload icon in the text editor:

2. Scenario Blueprint

Please export the scenario blueprint file to allow others to view the mapped variables in the module fields. At the bottom of the scenario editor, you can click on the three dots to find the Export Blueprint menu item.

3. Output Bundles of Modules

Please provide the output bundles of the modules by running the scenario (or get from the scenario History tab), then click the white speech bubble on the top-right of each module and select “Download input/output bundles”.

A. Upload as Text File

Save each bundle contents in your text editor as a bundle.txt file, and upload it here into this discussion thread.

B. Insert as Formatted Code Block

If you are unable to upload files on this forum, alternatively you can paste the formatted bundles.
These are the two ways to format text so that it won’t be modified by the forum:

  • Method 1: Type code block manually

    Add three backticks ``` before and after the content/bundle, like this:

    ```
    content goes here
    ```

  • Method 2. Highlight and click the format button in the editor

Providing the input/output bundles will allow others to replicate what is going on in the scenario even if they do not use the external service.

Following these steps will allow others to assist you here. Thanks!

Had another long post break my scenario again. Obviously editing this just to get past a long post each time is not ideal. The weird thing is, I’ve had this scenario for years and this problem (and non-functional regex replace) has only started in the last month. And I KNOW I tested it when I created it originally, and the replacement was working at the time.

I looked at your links and that’s great for not breaking in the middle of words. But it doesn’t solve “if” not working…

I don’t want to use a filter because there’s a lot of stuff going into the Discord embedded message that I don’t want to duplicate/maintain two separate versions just for message length!

bundle1.txt (4.9 KB)
bundle.json (14 Bytes)
blueprint.json (35.9 KB)

Okay… maybe this is why the if function doesn’t work? I don’t see a greater-than operator as an option.

image

The greater than operator is in the math tab (third).