Nodes/ComfyUI/Replace Text (Regex)
ComfyUI Node Runs on cloud

Replace Text (Regex)

When plain find-and-replace isn't enough

By Comfy-Org·Created 4 years ago·Updated about an hour ago· 129,943
Replace Text (Regex)
    • STRING
    string
    regex_pattern
    replace
    case_insensitivetrue
    multilinefalse
    dotallfalse
    count0

    Plain find-and-replace asks "find this exact text." Regex asks "find anything that looks like this pattern" - every phone number, every (word:1.3) attention weight, every trailing comma, every whitespace run. Replace Text (Regex) (RegexReplace) is the core node that does the pattern version, and it's the difference between hand-editing a prompt 20 times and fixing all 20 with one pattern.

    Reach for it when the text you're cleaning is variable. Real examples from prompt workflows: strip all the (word:1.2) emphasis markers out of a downloaded prompt (\([^)]*\) → empty), collapse doubled commas and spaces (,\s*,,), normalize line breaks, swap a model's tag dialect for another, or scrub a number out of an LLM's output. If the thing you're hunting is "any one of these characters in a row," regex is your tool and this node is how you apply it in the graph.

    How it works

    Under the hood it's Python's re.sub(pattern, replace, string, count=count, flags=flags) - the same regex engine that's been in Python for decades, so every pattern you've ever used in a script works here verbatim. It scans the whole string and replaces every match unless you cap it with count. The replacement string can use \1, \2 backreferences to pull in captured groups from your pattern - the classic "swap first and last name" move.

    The inputs are exactly what you'd expect: string (the text to process), regex_pattern, and replace (what matched text becomes). Then three flag toggles and a counter:

    • case_insensitive - defaults to true. This is the footgun of the family: unless you turn it off, your pattern dog will happily match "Dog," "DOG," and "dOg." Great when you want it, surprising when you don't.
    • multiline - off by default. Turns on ^ and $ matching at line boundaries instead of just the start/end of the whole string.
    • dotall - off by default. When on, . matches newlines too (its own tooltip says it plainly). Usually you want this off, because . crossing line breaks is how one sloppy pattern eats an entire prompt.
    • count - max replacements. 0 (default) means all of them; 1 means only the first, which is handy when your pattern matches too eagerly and you want to tame it.

    Output is a single STRING: the transformed text, ready to feed the next node.

    Common issues

    Regex bites in two places here. First, escaping: in the ComfyUI text field, a literal parenthesis or dot still needs \( or \. in your pattern, and backslashes in the replacement are interpreted - so \n in the replace box is a newline, not two characters. Second, the default case-insensitivity catches everyone at least once; if your replacement is oddly aggressive, check that toggle. Also note the count cap of 100 - you can't do 101+ replacements in one pass, which is a non-issue unless you're doing something very weird.

    If your find-and-replace needs no patterns - literally "replace 'cat' with 'dog'" - you don't want regex at all: use the sibling Replace Text (StringReplace), which is the plain version and can't misfire on a stray *. Regex is power; the plain node is the boring, safe one. Use the right tool.

    Categorytext

    Inputs (7)

    NameTypeDefaultDescription
    stringSTRING
    regex_patternSTRING
    replaceSTRING
    case_insensitiveoptBOOLEANtrue
    multilineoptBOOLEANfalse
    dotalloptBOOLEANfalseWhen enabled, the dot (.) character will match any character including newline characters. When disabled, dots won't match newlines.
    countoptINT00–100Maximum number of replacements to make. Set to 0 to replace all occurrences (default). Set to 1 to replace only the first match, 2 for the first two matches, etc.

    Outputs (1)

    NameTypeDescription
    STRINGSTRING