Nodes/JNComfy/Text Replace
ComfyUI Node

Text Replace

Find-and-replace for your prompts, regex optional

By jn-jairo·Created 2 years ago·Updated 2 years ago· 5
Text Replace
  • value
  • STRING
search
replace
regexfalse

You know when you've got a prompt that's 90% right and you just need to swap one word - or strip the "((masterpiece))" spam, or normalize some weird formatting? That's JN_TextReplace. It takes any value, does a search-and-replace, and hands back a string. Simple, but it's the kind of node that makes prompt engineering feel less like wrestling.

How it works

Four inputs, two modes:

  • value - anything. It gets converted to a string with str() first, so you can throw numbers or other types at it without ceremony.
  • search - the text to find.
  • replace - the text to substitute. Empty string deletes the match.
  • regex - the mode switch. Off (default) is a literal find-and-replace, exactly like a text editor: every occurrence of search becomes replace. On, it runs Python's re.sub(search, replace, value), which means search is a regular expression and replace can contain backreferences like \1.

Output is a STRING. Nothing fancy downstream - wire it into a text encoder, a filename builder, or another text node.

Where you'd use it

Prompt sanitation is the killer use. Normalize prompt fragments from different sources: strip (parentheses) weighting, collapse whitespace, replace underscores with spaces, or delete [garbage] tags wholesale. With regex on, you can do real cleanup - collapse multiple spaces with \s+" ", strip leading/trailing whitespace with ^\s+|\s+$"", or strip all digits with \d+"". It also slots into filename/path construction when you need to scrub characters a filesystem won't take.

It composes well with JN_TextConcatenation: concatenate fragments, then run one replace pass over the result to clean up whatever the joining introduced.

Install

Part of JNComfy. ComfyUI Manager → search "JNComfy" → Install, restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/jn-jairo/jn_comfyui

restart, and pip install -r requirements.txt inside the folder if you're going manual. This node is pure Python (str.replace / re.sub); the pack's heavy dependencies belong to the audio and face-restore sections and don't matter here.

Gotchas

  • Regex errors kill the run. A malformed pattern raises re.error and aborts the workflow with a stack trace. There's no graceful fallback, so test gnarly patterns in a scratch environment or start with the literal mode.
  • Escaping matters. In regex mode, a literal . is "any character" unless you write \.. If your search has dots or brackets and regex is on, expect surprises.
  • Empty search in literal mode is a no-op (Python's replace with "" matches nothing) - so "I'll leave it blank and it'll be fine" gives you an unchanged string, not an error. Verify with JN_Dump if you're unsure what came out.
  • Remember the pack patches ComfyUI on startup; console lines like "Failed to import module" after an update usually mean a patch collision, not a problem with this node.
CategoryJN/Primitive/Process

Inputs (4)

NameTypeDefaultDescription
value*
searchSTRING
replaceSTRING
regexBOOLEANfalse

Outputs (1)

NameTypeDescription
STRINGSTRING