Nodes/honda-nodes/πŸ”€ Text Replace
ComfyUI Node

πŸ”€ Text Replace

Strip the junk out of prompts and filenames

By AvarayΒ·Created 14 days agoΒ·Updated 12 days agoΒ· 0
πŸ”€ Text Replace
    • Output
    β—„textβ€”β–Ί
    β—„findβ–Ί
    β—„replace_withβ–Ί
    β—„use_regexfalseβ–Ί
    β—„ignore_casefalseβ–Ί
    β—„replace_alltrueβ–Ί

    Prompts arrive from everywhere in a mess: pasted from a gallery with masterpiece, best quality welded to the front, tagged with (worst quality:1.4) twice, full of underscores because someone normalised it for a filename, or carrying Windows-illegal characters because it was going to be a filename. Text Replace is the scrubber. Find something, replace it, done - and if you switch on regex, it becomes a surprisingly capable scrubber.

    How it works

    One re.sub or one str.replace, depending on Use Regex. With Replace All on (the default) it replaces every occurrence; turn it off and only the first one goes, which is exactly what you want for "strip a single leading tag".

    Two implementation details worth knowing because they change what patterns work.

    With regex on, the replacement string is passed to re.sub raw, so backreferences work: find (\d+) steps, replace with steps=\1, and the number survives. That's how you reformat rather than just delete.

    With regex off but Ignore Case on, it takes a different path - it escapes your find string and passes a lambda as the replacement, specifically so that a literal \1 in your replacement isn't interpreted. So backreferences only exist in regex mode. Literal mode is literal on both sides, which is the sane contract even if the implementation looks like two different code paths (it is).

    And the failure mode to know: invalid regex doesn't throw. It catches re.error, prints one line to the console, and returns the input unchanged. A typo'd pattern means your scrubber silently does nothing for the rest of the session. Empty find is also a no-op (returns the text untouched), which at least is a friendly default.

    The inputs and outputs that matter

    Text Input - forced input, must be wired. Find - the literal or pattern. Replace With - the replacement; can be empty, which is how you delete. Use Regex, Ignore Case, Replace All - three toggles, and between them they cover most of what you'll ever need.

    One output: Output, the modified STRING.

    The scrubbers I'd actually build with it:

    • Prompt cleanup. Find ,\s*, with regex on, replace with , - fixes the double commas that tag-style prompts accumulate. Run it twice, or add a second node, because ,,, needs two passes.
    • Weight syntax. Find \(([^:)]+):[\d.]+\) and replace with $1 - flattens (masterpiece:1.4) to masterpiece when you're moving a prompt to a model that doesn't do weights.
    • Underscores to spaces. Find _, replace with a space. Text Concatenate's whitespace option goes the other way, so between the two you can convert in either direction. This one earns its keep on tag models: Danbooru-style prompts are trained space-separated, and an underscored prompt is a quieter prompt.
    • Filename safety. Find [<>:"/\\|?*] with regex on, replace with _, and the result survives a Windows filesystem. Wire that between Text Concatenate and Save Image's filename and your prompt-derived filenames stop throwing errors.

    That last one is the highest-value use in the pack. Everything else here is convenience; that one prevents a failed save at 3am.

    How to install it

    Part of Avaray/honda-nodes - ComfyUI Manager β†’ Node Pack β†’ search Honda Nodes β†’ Install β†’ restart. Manual:

    cd ComfyUI/custom_nodes
    git clone --depth 1 https://github.com/Avaray/honda-nodes.git
    cd honda-nodes
    python install.py
    

    Pure stdlib; the installer is for the pack's jq and ime binaries. Requires ComfyUI 0.38.0 or newer (V3 schema API). No pip packages, no models.

    Common issues

    Nothing changed and there's no error. Three candidates: Find is empty, the pattern is case-mismatched (turn on Ignore Case), or your regex doesn't compile and it failed to the console. Open the console and look for the Invalid Regex in Text Replace line.

    Backslashes disappeared or doubled. The widget is a plain string. To match a literal backslash in regex mode you need \\ in the field; to match a literal dot you need \.. This is Python's re, not a shell glob.

    Only one occurrence replaced. Replace All is off. It's on by default, so you probably turned it off for something else and forgot.

    $1 came out literally. You're in literal mode (Use Regex off) - backreferences only work with regex on.

    It mangled a prompt I wanted intact. Regex is a sharp tool and this node won't warn you. Text Preview the output before you wire a scrubber into a live pipeline; watching it eat half a prompt on a preview node costs one run, discovering it after a 200-image batch costs an afternoon.

    Category⚑️ Honda Nodes/πŸ”€ Text

    Inputs (6)

    NameTypeDefaultDescription
    textSTRINGThe original text to modify (must be connected from another node).
    findSTRINGThe text or regex pattern to search for.
    replace_withSTRINGThe text to replace matches with.
    use_regexBOOLEANfalseIf enabled, treats the 'Find' field as a Python Regular Expression.
    ignore_caseBOOLEANfalseIf enabled, the search will be case-insensitive.
    replace_allBOOLEANtrueIf enabled, replaces all occurrences. If disabled, replaces only the first occurrence.

    Outputs (1)

    NameTypeDescription
    OutputSTRINGβ€”