Nodes/String Utils/Text Replacer
ComfyUI Node

Text Replacer

Find-and-replace for prompts, regex mode included

By exectails·Created 2 years ago·Updated 7 months ago· 3
Text Replacer
    • text
    text
    replace
    replacement
    modenormal

    This is the node in the String Utils pack with actual teeth. The formatters and converters are plumbing; Text Replacer is where you clean up, transform, and restructure text - and the mode switch to regex is what makes it more than a find-and-replace. If you've ever wanted to strip a tag out of every prompt, swap a character name across a workflow, or do a pattern-based rewrite, this is the node.

    What it does

    Three text inputs and a mode selector. Give it the source text, a replace pattern, and a replacement, and it returns the transformed string.

    • normal mode - literal string replacement. text.replace(replace, replacement). "John" → "Mr. Smith", everywhere it appears.
    • regex mode - re.sub(replace, replacement, text). The replace field is a regular expression, and replacement can use backreferences to captured groups.

    The README's regex example is the one to internalize: with replace="([0-9]+) is" and replacement="\1 is in fact", the text "42 is the answer" becomes "42 is in fact the answer" - the \1 pulls the captured 42 into the replacement. That pattern - match a chunk, keep part of it, swap the rest - is genuinely powerful for prompt cleanup.

    The inputs that matter

    • text - what you're transforming.
    • replace - what to find: a literal string in normal mode, a regex pattern in regex mode.
    • replacement - what to put in its place.
    • mode - dropdown, normal (default) or regex.

    Output: text, the transformed STRING.

    How to install it

    Part of exectails/comfyui-et_stringutils ("String Utils"). ComfyUI Manager → search "String Utils", or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/exectails/comfyui-et_stringutils
    

    Restart, and it's under exectails/Strings. No dependencies, no model downloads - the only library it touches is Python's built-in re module.

    Common issues

    • Normal mode is literal. Put "(" in the replace field in normal mode and it looks for a literal parenthesis. Regex metacharacters only act like patterns in regex mode. If your text contains regex characters and you're not intending a pattern, stay in normal mode.
    • Invalid regex = crash. A malformed pattern (say, an unclosed () raises an error and kills the run. Test your pattern elsewhere first if it's gnarly.
    • No matches is not an error. re.sub and str.replace both return the text unchanged when nothing matches - the node just passes it through. That's usually what you want, and it means a missing tag in your data won't take the whole workflow down.
    • Backreference syntax. \1 works in the replacement for regex mode (that's what the README shows), and \g<1> works too. Note those backslashes are meaningful only in regex mode - in normal mode \1 is a literal backslash-one.

    Use it to strip stray tags, normalize whitespace, swap names, or build a regex-powered cleaning stage between your prompt source and the encoder. It's the utility node that quietly saves you from editing prompts by hand.

    Categoryexectails/Strings

    Inputs (4)

    NameTypeDefaultDescription
    textSTRINGThe text to replace in.
    replaceSTRINGThe text to replace.
    replacementSTRINGThe text to replace with.
    modeCOMBOnormalThe mode to use for replacing.

    Outputs (1)

    NameTypeDescription
    textSTRING