Nodes/ComfyUI-JDCN/LogicUtil_Replace String
ComfyUI Node

LogicUtil_Replace String

Regex replace — read the fine print before you wire it up

By daxcay·Created 2 years ago·Updated about a year ago· 159
LogicUtil_Replace String
    • STRING
    String
    Regex
    ReplaceWith

    LogicUtil_Replace String does text replacement in the graph, but with a critical detail hidden in the fine print: the "find" field is a regular expression, not a plain text search. Three inputs - String, Regex, and ReplaceWith - and one STRING out, produced by Python's re.sub(Regex, ReplaceWith, String). If you treat Regex like a plain "find this exact text" field, you will be confused within minutes.

    How it works

    That regex behavior is the whole story. It means the node is genuinely powerful and genuinely easy to misuse:

    • Want to strip all digits from a prompt? Regex = \d+ - gone.
    • Want to swap portrait for landscape? That works as plain text too, as long as you remember regex metacharacters. Search for a.b and it'll match acb as well as a.b, because . is a wildcard.
    • Empty Regex is a trap. re.sub('', X, text) inserts ReplaceWith between every character. Set Regex to empty and your string comes back with the replacement sprinkled throughout. It won't error - it'll just look wrecked.
    • Need to match a literal $ or (? Escape it: \$, \(. This is the single most common cause of "replace did nothing" - the metacharacter matched something you didn't see.

    There's no case-insensitivity flag and no "replace all vs first" toggle - re.sub always replaces every match, case-sensitively. If you need case-insensitive matching, include (?i) at the start of your pattern.

    Where you'd actually use it: cleaning scraped or imported prompts, normalizing filenames before they hit a saver node, or building a prompt-rewriter that strips one style token and injects another. It plays well with the rest of the family - Manual Choice String for options, Replace String for rewriting, Merge String for assembly. The whole family is a port of aria1th's ComfyUI-LogicUtils riding inside the JDCN pack from Daxton Caylor and Jerry Davos; the README documents the file and latent batch nodes and never mentions this one, so the source is the doc - and the source is unambiguously regex.

    Install

    Install is the standard JDCN routine: ComfyUI Manager → "Install Custom Node" → search JDCN → install → restart, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/daxcay/ComfyUI-JDCN
    cd ComfyUI-JDCN && pip install -r requirements.txt
    

    Only piexif on the requirements list, no models to fetch. And the fastest sanity check when a replacement looks wrong: put a literal, non-meta string in Regex and confirm it works, then add metacharacters back one at a time.

    CategoryLogicUtil

    Inputs (3)

    NameTypeDefaultDescription
    StringSTRING
    RegexSTRING
    ReplaceWithSTRING

    Outputs (1)

    NameTypeDescription
    STRINGSTRING