ComfyUI Node

replace

Replace — find-and-replace for your strings, with a count dial

By StableLlama·Created about a year ago·Updated 4 days ago· 48
replace
    • STRING
    string
    old
    new
    count-1

    The text in a ComfyUI workflow is never final. You want to swap && for and, clean a token out of a prompt, replace a placeholder with an actual value, or fix a spacing quirk that showed up in every line you loaded from a file. replace is the node that does it: swap every occurrence of one substring for another, or limit the surgery to the first N occurrences.

    It's the string node you'll use on almost every workflow that processes text - prompt templating (swap {subject} for the actual subject), sanitizing output, converting separators between formats. If you've ever done find-and-replace in an editor, you already know this node; it's the same operation, in the graph.

    How it works

    Python's str.replace(old, new, count). With the default count=-1, every occurrence of old is replaced with new. Set count to a positive number and only the first that many occurrences change, scanning left to right.

    • "a b a b".replace("a", "X")"X b X b"
    • "a b a b".replace("a", "X", 1)"X b a b"
    • "hello".replace("l", "L")"heLLo"

    One Python quirk to know: if old is an empty string, replace inserts new between every character - "hi".replace("", "-")"-h-i-". It's technically correct Python, and it's almost never what you wanted.

    Inputs and outputs

    Three required inputs:

    • string - the text to edit.
    • old - the substring to find.
    • new - the replacement.

    Plus one optional:

    • count - INT, default -1. Limit to the first N replacements; -1 means replace all.

    Output is one STRING - the edited copy.

    Installing it

    Ships in the Basic data handling pack by StableLlama. ComfyUI Manager, search "Basic data handling", install, restart. Manual route:

    cd ComfyUI/custom_nodes
    git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
    

    Pure Python, zero dependencies, no downloads. Under Basic/STRING in the menu.

    Common issues

    Two traps. First, replace is literal, not a pattern: it matches the exact substring, so replace("a", "X") on "A" does nothing - case matters, and there's no wildcard. If you need regex, this pack has a separate regex module; if you need case-insensitive matching, lowercase both sides first. Second, the empty-old behavior above will silently insert characters everywhere if you leave old blank. And remember count counts from the left - with count=1, the first occurrence changes, so if you actually wanted the last one replaced, that's a job for splitting from the right with rsplit and rejoining.

    CategoryBasic/STRING

    Inputs (4)

    NameTypeDefaultDescription
    stringSTRING
    oldSTRING
    newSTRING
    countoptINT-1

    Outputs (1)

    NameTypeDescription
    STRINGSTRING