Hey @lonacunn
// Get the text from the incoming item(s). // This assumes the text is in a property called “text”. // Adjust the path if your field is named differently. const rawText = $input.first().json.text;
// Regex to match each block: “n” followed by a number, then everything until the next “n” or end. // The “s” flag makes dot match newlines as well. const regex = /(n\d+)(.*?)(?=n\d+|$)/gs; const blocks = ;
let match; while ((match = regex.exec(rawText)) !== null) { const number = match[1]; // e.g., “n1” const content = match[2]; // the text after the number blocks.push({ number: number, content: content.trim() // optional: trim whitespace }); }
// Return each block as a separate item return blocks.map(block => ({ json: block }));
What it does:
· $input.first().json.text – accesses the text from the first incoming item (adapt if your field is different). · The regex /(n\d+)(.*?)(?=n\d+|$)/gs finds each n1, n2, etc., captures the number and the content, and stops right before the next nX or the end. · Each matched block becomes an object with number and content. · Finally, we map these objects into n8n items (each with a json property).
3. Loop over the items
After the Code node, you can use a Loop node (or a Split In Batches node with batch size 1) to iterate over each generated item.
· Add a Loop node. · In its settings, set Mode to “Loop over items”. · Inside the loop, connect an ElevenLabs node.
4. Configure the ElevenLabs node inside the loop
In the ElevenLabs node, you’ll reference the current item’s content. Use expressions like:
· Text: {{ $json.content }} (or {{ $json.text }} if you used a different property name) · Voice ID: whatever you set · Output format: mp3, etc.
Make sure to check Attach Binary Data if you want the audio as a binary file for later use.
5. Save each audio file (optional)
If you want to save each file separately, you can add a Write File node after ElevenLabs, also inside the loop. Use an expression for the file name, e.g.:
· File Path: /tmp/audio_{{ $json.number }}.mp3
Now, when the loop runs, it will generate one audio file per block.
6. Example output of the Code node
For input text:
n1 Hello world, this is first part. n2 Second part here. n3 Final part.
The Code node will produce three items:
Item json 1 { “number”: “n1”, “content”: “Hello world, this is first part.” } 2 { “number”: “n2”, “content”: “Second part here.” } 3 { “number”: “n3”, “content”: “Final part.” }
7. Customising for your text format
If your text uses newlines or different separators, the regex will still work because the s flag makes dot match newlines. If the numbers are not exactly like n1, n2 (e.g., n1., n1:), you can adjust the regex accordingly. For example, to allow punctuation after the number: /(n\d+[.:]?)(.*?)(?=n\d+|$)/gs.
8. Important notes
Make sure the text property in the incoming data is correctly named. If it comes from a Manual Trigger with a text field, the above code works. · If the text is very large, the regex might be slower, but it’s fine for typical TTS use cases. · The Code node runs once and creates all items; the loop then processes each item sequentially. If you need parallel processing, you could use Split In Batches with batch size >1, but ElevenLabs may have rate limits.
Hope this helps!