Nodes/ComfyUI_StringOps/多次出现依次替换🐠meeeyo.com
ComfyUI Node

多次出现依次替换🐠meeeyo.com

Replace the same string N times — each time with a different value

By MeeeyoAI·Created 2 years ago·Updated about a year ago· 209
多次出现依次替换🐠meeeyo.com
    • STRING
    original_text
    replacement_rule

    Normal find-and-replace swaps every match for the same thing. ReplaceMultiple does the thing plain replace can't: it replaces the first occurrence with one value, the second with another, and so on. Same search string, a sequence of replacement values, applied in order. If you've ever wanted to turn a repeated placeholder in a template into a numbered list of items, this is the node.

    The Chinese display name (多次出现依次替换, "repeated occurrences replaced in sequence") says it exactly. It's part of ComfyUI_StringOps, and it's the fancier cousin of the pack's ReplaceNthOccurrence - that one targets a single position; this one walks through a whole list.

    How it works

    Two inputs:

    • original_text - the multiline source.
    • replacement_rule - a string with one | and one or more commas. Format: search|value1,value2,value3.

    So with rule X|A,B,C and text X then X then X, you get A then B then C. The node splits the rule on | to get the search string, splits the right side on , to get the replacement list, then walks the text replacing each match with the next item in the list.

    Two behaviors worth knowing. If the list runs out - text has four Xs but only three replacements - the fourth match is left alone, not deleted and not replaced. And empty list entries are filtered out, so X|A,,C treats the middle as empty and skips it, shifting your mapping. For most use you'll want clean, non-empty values.

    Where you'd use it

    The killer use is templates. Build a prompt template with a repeated token, then feed it a comma-separated list to fill in:

    • text: scene: X, camera: X, lighting: X
    • rule: X|forest,handheld,overcast

    You get scene: forest, camera: handheld, lighting: overcast. Same pattern works for filling numbered slots, generating a list from a set, or converting a tag,tag,tag string into separate slots - which pairs nicely with the pack's TextToList when your source is line-based instead.

    Output is a single STRING.

    The traps

    The comma is the separator, so your replacement values can't contain commas - a value like "New York, NY" will split into two. If commas are essential, replace them with a placeholder first or use ReplaceNthOccurrence per position instead. Also note the rule uses the first | only, so your search string can't contain a pipe either (unusual, but if it does, the split misbehaves and you get an empty string back via the ValueError handler - again, a silent empty output, not an error).

    Installing it

    Pack standard: ComfyUI Manager → ComfyUI_StringOps, or

    cd ComfyUI/custom_nodes
    git clone https://github.com/MeeeyoAI/ComfyUI_StringOps.git
    

    Restart. No extra dependencies for this one - it's regex, which ComfyUI already has.

    The honest take

    This is one of the more clever nodes in the pack, and once you've used it for template filling you'll keep a mental bookmark on it. Just remember the rule grammar (search|a,b,c) and the no-comma constraint, and it'll do in one node what would otherwise be a stack of chained replacements.

    CategoryMeeeyo/String

    Inputs (2)

    NameTypeDefaultDescription
    original_textSTRING
    replacement_ruleSTRING

    Outputs (1)

    NameTypeDescription
    STRINGSTRING