Nodes/ComfyUI_yanc/๐Ÿ˜ผ> Text Replace
ComfyUI Node

๐Ÿ˜ผ> Text Replace

Swap one word for another inside a prompt string, no scripting node needed

By ALatentPlaceยทCreated 2 years agoยทUpdated 2 years agoยท 80
๐Ÿ˜ผ> Text Replace
    • text
    โ—„textโ€”โ–บ
    โ—„findโ€”โ–บ
    โ—„replaceโ€”โ–บ

    It's a find-and-replace node: pass it a text string plus a find value and a replace value, and it swaps every occurrence of find with replace before the text moves on. That's the entire feature set, and that's the point - it's the one you reach for instead of building out a Python-eval or regex node just to templatize a prompt.

    The obvious use is a template prompt with a placeholder token - write your prompt once as "a photo of {subject} standing in a forest" and use Text Replace to swap {subject} for whatever value is coming out of another node upstream (a Text Pick Random Line, a batch loop, an API input). It also comes in handy for cleanup: stripping out a placeholder LoRA trigger word you don't want in the final saved caption, or normalizing inconsistent tag formatting (replacing underscores with spaces from a booru-tag source, for instance).

    How it works

    Straight string substitution - every instance of find inside text gets swapped for replace. It's not regex, so there's no pattern matching, capture groups, or wildcards; what you type in find is matched literally.

    The inputs and outputs that matter

    • text - the source string to operate on.
    • find - the literal substring to search for.
    • replace - what to substitute in its place. Leave it empty to just delete every occurrence of find instead of substituting something else.

    One output: text, the result - chain it into a CLIP Text Encode, another Text Replace for a second substitution, or a Save Image filename.

    How to install it

    Via ComfyUI Manager: search "yanc" or "YANC - Yet Another Node Collection," install, restart. Manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/ALatentPlace/ComfyUI_yanc
    

    then restart. Pure string manipulation, no extra dependencies or downloads.

    Common issues & troubleshooting

    Nothing gets replaced. Since matching is literal, capitalization and exact spacing both matter - find set to "Subject" won't match "subject" in the text, and "a cat" (double space) won't match "a cat" (single space). Check your find value character-for-character against what's actually in text.

    You need multiple replacements. One Text Replace handles one find/replace pair. For a template with several placeholders ({subject}, {location}, {style}), chain multiple Text Replace nodes in sequence, each handling one placeholder - the output text of one feeds straight into the text input of the next.

    You wanted pattern matching, not exact-text matching. If you need something like "replace any number" or "replace anything inside brackets," that's regex territory and this node doesn't do it - it's intentionally the simple literal-substitution tool, not a scripting node. For anything pattern-based you'd need a more general text/expression node elsewhere in the ecosystem.

    CategoryYANC/๐Ÿ˜ผ Text

    Inputs (3)

    NameTypeDefaultDescription
    textSTRINGโ€”
    findSTRINGโ€”
    replaceSTRINGโ€”

    Outputs (1)

    NameTypeDescription
    textSTRINGโ€”