Nodes/ComfyUI-gen2/Gen2 StringReplace
ComfyUI Node

Gen2 StringReplace

The boring node that makes dynamic prompts work

By petmycat·Created 7 months ago·Updated 12 days ago· 23
Gen2 StringReplace
    • output_string
    input_string
    search
    replace

    String nodes are the unsung workhorses of ComfyUI. Once you start building workflows that manipulate prompts - wildcards, LoRA-aware prompt templates, cleanup of a pasted-in caption - you need a way to rewrite text mid-graph, and Gen2 StringReplace is exactly that: replace every occurrence of a search string with a replacement, case-sensitively, all of them. It's the str.replace() you'd write in two lines if the graph would let you, packaged as a node.

    Inputs and output

    Three inputs, all STRING:

    • input_string - the text to process (multiline).
    • search - the exact substring to find. Case-sensitive, and literal: no regex. cat won't match Cat.
    • replace - what to swap in. Can be empty, which effectively deletes the matches.

    The output is output_string - the processed text. Wire it into whatever eats a string: a CLIP text encode, a filename prefix builder, a conditioning input, another string node.

    Two behaviors worth knowing because they surprise people:

    • All occurrences are replaced, not just the first. That's what "replace all" means here and it's the thing to remember if your search string shows up more than once.
    • An empty search returns the input unchanged. The author guards against the degenerate case where "".replace("", "x") would do something silly in Python - you don't get a silent injection of text everywhere. That's a thoughtful edge case to handle, honestly.

    Why you'd actually reach for it

    Real example: you have a prompt template with a placeholder like __subject__ and you want to swap in a value from another node without hand-editing the template every run. Wire the template into input_string, set search to __subject__, feed replace from whatever generates the subject - done. Same trick works for stripping tokens out of captions, normalizing LoRA trigger phrases, or building a filename prefix that changes per run. It's a glue node: nothing it does is glamorous, but once it's in your toolbox you'll use it in places you didn't expect.

    Installation

    Part of petmycat/ComfyUI-gen2. Install via ComfyUI Manager (search "ComfyUI-gen2"), or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/petmycat/ComfyUI-gen2
    cd ComfyUI-gen2
    pip install -r requirements.txt
    

    Restart, and it's under the Gen2/Utils category. No extra dependencies - this is one of the "loads with zero setup" sections of the pack.

    Common issues

    • "It didn't replace anything." Check case. It's case-sensitive by design, and if your search string has trailing whitespace from a paste, that whitespace is part of the match.
    • "It replaced too much." Remember it replaces every occurrence. If you wanted only the first, you need a regex-capable node - this one deliberately isn't.
    • No search term. Empty search = no-op, as designed. If you wired search from a node that returned an empty string, that's your answer.

    Nothing to tune, nothing to break. You'll forget it exists until the moment you need it, which is the right way for a utility node to live.

    CategoryGen2/Utils

    Inputs (3)

    NameTypeDefaultDescription
    input_stringSTRING
    searchSTRING
    replaceSTRING

    Outputs (1)

    NameTypeDescription
    output_stringSTRING