Nodes/ComfyUI-Custom-Scripts/String Function 🐍
ComfyUI Node Runs on cloud

String Function 🐍

Append or regex-replace text without writing Python

By pythongosssssΒ·Created 3 years agoΒ·Updated 6 months agoΒ· 3,150
String Function 🐍
    • STRING
    β—„actionβ–Ύβ–Ί
    β—„tidy_tagsβ–Ύβ–Ί
    β—„text_aβ€”β–Ί
    β—„text_bβ€”β–Ί
    β—„text_cβ€”β–Ί

    This is the node for when you want to build a prompt (or any text) out of pieces instead of one giant text box, or you need to swap a word out programmatically instead of retyping it by hand every time. It does two things - append and replace - and it does both without asking you to write any actual code.

    Like the rest of ComfyUI-Custom-Scripts, it's from pythongosssss, one of the co-founders of Comfy Org. It's a small, unglamorous node, but it's the kind of thing that ends up load-bearing once your prompts get complicated enough to want structure.

    Append mode: gluing text together

    Feed text_a, text_b, and text_c (all optional multiline strings) and set action to append. It concatenates them in order. The interesting part is tidy_tags (yes/no): switch it on and the node inserts commas between the non-empty parts for you, matching the comma-separated tag style most SD 1.5/SDXL/Illustrious prompts use - so you get 1girl, blue hair, smiling instead of 1girl blue hairsmiling jammed together with no separator. It only adds commas where the join actually needs one; a field you leave genuinely empty doesn't leave a stray comma behind.

    This is useful for building a prompt out of swappable chunks - subject in text_a, style in text_b, quality tags in text_c - and reordering or replacing any one piece without retyping the whole thing.

    Replace mode: find, replace, with real regex

    Switch action to replace and the roles change: text_a is the source text, text_b is what to search for, text_c is what to replace it with. Plain text works as a literal substring match. For regex, wrap the pattern in slashes - /your regex here/ - and you can reference capturing groups in text_c using \1, \2, and so on.

    That makes this node capable of things a plain find-replace can't do: stripping a trigger word out of a Lora's example prompt before reusing it elsewhere, swapping 1girl for 1boy across a whole prompt in one controlled operation, or restructuring a caption pulled from an interrogator into a different word order using capture groups.

    Output

    A single STRING - not a list, one value out - ready to wire into a CLIPTextEncode, another String Function for chaining multiple operations, or a Show Text node to check the result before it goes further.

    Installing it

    • ComfyUI Manager - search "ComfyUI-Custom-Scripts", install, restart.
    • Manual - cd ComfyUI/custom_nodes && git clone https://github.com/pythongosssss/ComfyUI-Custom-Scripts, restart.

    No models, no extra Python dependencies.

    Common issues

    Replace isn't matching anything. The most common trap: you typed something that looks like a regex pattern (\d+, say) into text_b without wrapping it in slashes. Without the slashes, it's treated as a literal string, so it goes looking for the exact text \d+ rather than actually matching digits. Wrap the pattern: /\d+/.

    Commas look wrong in append mode. tidy_tags only inserts a separator between fields that actually have content. If a field looks "empty" but has a stray space in it, the node may treat it as non-empty and the comma placement won't look right. Clear the field completely if you want it skipped.

    Capture groups aren't substituting correctly. The group numbers in text_c (\1, \2, ...) have to match what your pattern in text_b actually captures with parentheses. If nothing's grouped in the pattern, \1 in the replacement has nothing to pull from. Test the regex on its own before chaining several String Function nodes together - debugging a multi-stage text pipeline after the fact is a lot harder than catching a bad pattern at step one.

    Categoryutils

    Inputs (5)

    NameTypeDefaultDescription
    actionCOMBO2 options: append, replace
    tidy_tagsCOMBO2 options: yes, no
    text_aoptSTRINGβ€”
    text_boptSTRINGβ€”
    text_coptSTRINGβ€”

    Outputs (1)

    NameTypeDescription
    STRINGSTRINGβ€”