Nodes/Bjornulf_custom_nodes/๐Ÿ“โžœ๐Ÿ“ Replace text
ComfyUI Node

๐Ÿ“โžœ๐Ÿ“ Replace text

Find-and-replace with regex, and it strips Ollama's <think> tags

By justUmenยทCreated 2 years agoยทUpdated about a year agoยท 545
๐Ÿ“โžœ๐Ÿ“ Replace text
    • STRING
    โ—„input_textโ€”โ–บ
    โ—„search_textโ€”โ–บ
    โ—„replace_textโ–บ
    โ—„replace_count0โ–บ
    โ—„use_regexfalseโ–บ
    โ—„case_sensitivetrueโ–บ
    โ—„trim_whitespacenoneโ–บ
    โ—„multiline_regexfalseโ–บ

    A plain find-and-replace node, but with enough options bolted on to handle the messy real-world cases a simple swap can't. The concrete example the pack's own changelog calls out is worth leading with: reasoning models routed through the pack's Ollama nodes sometimes wrap their internal reasoning in <think>...</think> tags before the actual answer, and this node - with regex and multiline matching turned on - is what people use to strip that block out before the text hits a display node or a TTS voice.

    How it works

    input_text is what you're editing, search_text is what to look for, replace_text is what to put in its place. use_regex switches from a literal string match to a full regular expression, which is what makes the <think> tag removal possible in the first place - you can't reliably strip an entire multi-line block with a plain string match, but a regex pattern spanning newlines can, provided you also flip on multiline_regex so the pattern matches across line breaks instead of stopping at each one. replace_count caps how many matches get replaced - leave it at the default and it replaces every occurrence; set it to a specific number and it stops after that many. case_sensitive is on by default, matching exact case only. trim_whitespace runs after the replace and cleans up leftover whitespace at the start, end, or both sides of the result - useful because stripping a tag out of the middle of text often leaves an awkward gap or leading space behind.

    The inputs and outputs that matter

    • input_text / search_text / replace_text (STRING, multiline) - the core find-and-replace trio.
    • use_regex (BOOLEAN, default false) - switch to regex matching for anything a literal string can't express, like matching across a tag pair.
    • multiline_regex (BOOLEAN, default false) - needed alongside use_regex if the thing you're matching spans multiple lines.
    • replace_count (INT, default 0) - 0 replaces every match; set a specific number to cap how many get replaced.
    • trim_whitespace (enum: none / left / right / both) - cleans up stray whitespace left behind after the replace.
    • Output: STRING - the result, ready to feed onward.

    How to install it

    Via ComfyUI Manager: search Bjornulf_custom_nodes, install, restart. Manually:

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

    then restart ComfyUI. No special dependency for this specific node - it's plain string/regex handling - but you're installing the whole 170-node pack and its shared requirements.txt regardless.

    Common issues & troubleshooting

    A regex pattern that works fine testing elsewhere doesn't match here. The most common cause is forgetting multiline_regex - if your target spans more than one line (like a <think>...</think> block that wraps across several lines), a standard regex . won't match the newlines inside it unless you flip that toggle on alongside use_regex.

    Replace works but leaves an awkward blank line or leading space. That's the expected residue of cutting a chunk out of the middle of a text block - set trim_whitespace to both (or whichever side applies) to clean it up automatically instead of chaining on another node just for that.

    Case-sensitive matching silently fails to find an obvious match. If search_text doesn't seem to find anything even though you can see it in input_text, check capitalization first - case_sensitive defaults to true, so "Hello" and "hello" won't match each other unless you turn it off.

    CategoryBjornulf

    Inputs (8)

    NameTypeDefaultDescription
    input_textSTRINGโ€”
    search_textSTRINGโ€”
    replace_textSTRINGโ€”
    replace_countINT00โ€“1000Number of replacements (0 = replace all)
    use_regexBOOLEANfalseโ€”
    case_sensitiveBOOLEANtrueWhether the search should be case-sensitive
    trim_whitespaceCOMBOnoneRemove whitespace around the found text
    multiline_regexBOOLEANfalseMake dot (.) match newlines in regex

    Outputs (1)

    NameTypeDescription
    STRINGSTRINGโ€”