Text Replace
Find-and-replace for your prompts, regex optional
- value
- STRING
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
searchbecomesreplace. On, it runs Python'sre.sub(search, replace, value), which meanssearchis a regular expression andreplacecan 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.errorand 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 andregexis on, expect surprises. - Empty
searchin literal mode is a no-op (Python'sreplacewith""matches nothing) - so "I'll leave it blank and it'll be fine" gives you an unchanged string, not an error. Verify withJN_Dumpif 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.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| value | * | — | |
| search | STRING | — | |
| replace | STRING | — | |
| regex | BOOLEAN | false | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| STRING | STRING | — |