Nodes/SaltAI-Open-Resources/Regex Search and Replace
ComfyUI Node

Regex Search and Replace

Pattern-based find-and-replace for your prompts

By get-salt-AI·Created 2 years ago·Updated 2 years ago· 84
Regex Search and Replace
    • replaced_text
    text_input
    regex_pattern
    replacement_text

    Prompt and caption text in ComfyUI workflows gets messy fast - trigger words that need stripping, placeholder tokens that need swapping, LoRA tags that need cleaning out before you save a caption to disk. SAIStringRegexSearchReplace is the node for that: a plain, no-frills regex find-and-replace you drop into any text pipeline.

    What it does

    Three multiline STRING inputs: text_input is the text you're editing, regex_pattern is the pattern to search for, and replacement_text is what replaces each match. The output, replaced_text, is the result. That's the whole node - every match of the pattern in the input gets swapped for the replacement text, all matches, not just the first.

    Since ComfyUI custom nodes run on Python, this behaves like Python's own regex engine: standard regex syntax, and if you need to reuse part of a match in your replacement, that's the Python-style group syntax (\1, \g<name>) rather than JavaScript's $1. Worth knowing if you're used to a different language's regex flavor and your replacement isn't inserting what you expected.

    Where it's useful

    The obvious use is prompt cleanup: stripping a trigger word before you log a prompt somewhere public, normalizing whitespace, swapping a placeholder token ({subject}) for an actual value pulled from elsewhere in your graph. It pairs naturally with this pack's own SAIStringRegexSearchMatch - use match to find something first if you need to branch on whether a pattern exists, use replace when you already know you want to swap it.

    Installing it

    ComfyUI Manager: search SaltAI-Open-Resources. Manual:

    cd ComfyUI/custom_nodes
    git clone https://github.com/get-salt-AI/SaltAI
    

    Restart ComfyUI. No dependencies beyond what's already standard in a Python environment - regex is part of the standard library, so there's nothing extra to install for this node specifically.

    Common issues

    The single most common mistake is treating the pattern field like a glob or a simple wildcard - *.txt-style thinking doesn't work here, you need actual regex syntax (. matches any character, * means "zero or more of the previous thing," special characters like ., (, ), [, ] need escaping with a backslash if you want them literally). If a replace silently does nothing, the pattern almost certainly isn't matching anything in your input - test the pattern somewhere with instant feedback (a regex tester, or this pack's own search-and-match node wired to a preview) before wiring it into a long pipeline where a silent no-op is easy to miss. And if you're trying to reference a captured group in the replacement and it's coming out literally as \1 instead of the matched text, double check you actually wrapped the part of your pattern you want to capture in parentheses - an ungrouped pattern has nothing for \1 to refer to.

    CategorySALT/String/Process/Regex

    Inputs (3)

    NameTypeDefaultDescription
    text_inputSTRING
    regex_patternSTRING
    replacement_textSTRING

    Outputs (1)

    NameTypeDescription
    replaced_textSTRING