ComfyUI Node Runs on cloud

String Replace

Find-and-replace on text mid-graph

By Derfuu·Created 3 years ago·Updated 2 years ago· 455
String Replace
    • TEXT
    Text
    Pattern
    Replace_With
    Mode

    String Replace does exactly what it says: swap a substring for something else, inside a workflow, before the text goes anywhere. Useful for a prompt template with a placeholder token that gets filled in dynamically, or for cleaning up a string coming out of another node (a stray trigger word, unwanted formatting) before it hits a CLIP Text Encode. It's one of the string-handling nodes in Derfuu_ComfyUI_ModdedNodes, a math-and-utility pack from 2023, back before ComfyUI had many native ways to manipulate text mid-graph rather than typing a final prompt straight into a box.

    The pack also has a DF_String_Replace doing the same job under the current DF_-prefixed naming convention - same fields, same behavior. String Replace (this one) is the older, unprefixed name. If you inherited a workflow using either, leave it be; either works.

    How it works

    You give it a string to operate on, a pattern to search for, and a replacement, plus a mode that decides how the pattern gets interpreted.

    The inputs and outputs that matter

    • Text (STRING) - the string you're transforming.
    • Pattern (STRING) - what to look for.
    • Replace_With (STRING) - what to substitute it with.
    • Mode - Strict or RegEx.
    • Output: TEXT (STRING) - the result, with every match swapped.

    Mode is the one setting worth getting right. Strict treats Pattern as a literal substring and replaces every exact occurrence - type "cat", get every "cat" swapped for Replace_With, nothing fancier. RegEx treats Pattern as an actual regular expression, so wildcards and character classes work, but it also means any regex-special character in your pattern - parentheses, brackets, a bare period - gets interpreted as regex syntax instead of a literal character. That's the most common source of "why didn't this match": someone left Mode on RegEx while typing what they meant as plain text. If you just want ordinary find-and-replace, stick with Strict.

    Where it fits

    This shows up in prompt-templating setups - a base string with a placeholder like {subject} swapped for a computed or randomized value elsewhere in the graph - or as a cleanup step stripping something unwanted before the final prompt reaches the encoder. Pairs naturally with a string concat node when you're assembling a prompt from a few pieces instead of one static block of text, and with To text (Debug) right afterward to confirm the replace actually did what you expected before it burns a generation.

    How to install it

    ComfyUI Manager: search Derfuu_ComfyUI_ModdedNodes, install, restart. Manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Derfuu/Derfuu_ComfyUI_ModdedNodes
    

    then restart. No models, no unusual dependencies.

    Common issues & troubleshooting

    Replacement isn't matching. Check Mode first - the most common mistake is leaving it on RegEx while typing a literal pattern that happens to contain a regex-special character, which silently changes what gets matched. Switch to Strict unless you specifically need pattern matching.

    Node not visible after install. Restart the server and hard-refresh the browser - a stale frontend cache, not a real install failure.

    Node missing or renamed after a pack update. Derfuu's string-handling nodes specifically have a documented history of disappearing across updates - a longtime user of the pack described its text-handling nodes getting deleted outright on an update rather than kept as a labeled legacy version, breaking downloaded workflows that used them with no warning. If a workflow you rely on uses string nodes from this pack, check the GitHub repo before updating; a ModuleNotFoundError mentioning Derfuu_ComfyUI_ModdedNodes_legacy means a duplicated folder, so keep exactly one, named Derfuu_ComfyUI_ModdedNodes.

    CategoryDerfuu_Nodes/Functions/String Operations

    Inputs (4)

    NameTypeDefaultDescription
    TextSTRING
    PatternSTRING
    Replace_WithSTRING
    ModeCOMBO2 options: Strict, RegEx

    Outputs (1)

    NameTypeDescription
    TEXTSTRING