How to split a string after 30’000 chars

Hey community,

does anybody knows how to split a string after a specific number? I imagine with a code node (tried to create the code but failed). Or does anyone has an even easier solution?

Thx in advance!

Marc

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

You can use the js fucntion “substring” to get part of a string, to get the text from the beginning until the 30000 char you would use:

.substring(0,30000)

if you want a array which you can later split into multiple items you could use:

.match(/.{1,30000}/g)

the 1 in the regex is the minimum characters per part and the 30000 the maximum char count.

Here is a demo workflow with both options as expressions in a set node:

I hope this helps

1 Like

Thanks @FelixL . I tested it and it’s working when i have a text block. But with my sample data, it’s creating way more array items. I think it’s always creating one when there is a new line.

Sample text:

<!DOCTYPE html><html lang="en" dir="ltr"><head>\n<meta charset="utf-8">\n<meta http-equiv="x-ua-compatible" content="ie=edge">\n<meta name="referrer" content="origin">\n<meta name="apple-mobile-web-app-status-bar-style" content="#B82F00">\n

Screenshot:

I am not a regex wizard, but after some trial and error I got this:

{{ $json.text.match(/[\s\S]{1,30000}/g) }}

it works with your sample data, but please test it thoroughly with some more data ^^

1 Like

Thx. For some reason, for the 3rd, 4th, 5th and later array items, the chunks get smaller and smaller…