String Replace (mtb)
Find-and-replace for prompt and text strings in ComfyUI
- STRING
Sometimes you don't need a clever node, you need find-and-replace. That's this one. You feed it a string, tell it what to swap out and what to swap in, and it hands back the edited string. No model, no GPU, nothing to download - it's plumbing.
Where it earns its keep is prompt wrangling. Say you've built a prompt string upstream from a wildcard node, a template, or an LLM caption, and it keeps leaking a token you don't want - a trailing "photograph of", a stray "BREAK", a placeholder like {subject} you forgot to fill. Instead of rebuilding the whole chain, drop a String Replace in the wire and kill it. It's also handy for building prompt variants: keep one base prompt and swap "daylight" → "night", "red dress" → "blue dress", and route each version to its own sampler.
It comes from MTB Nodes (comfy_mtb) by melMass, one of the older broad utility packs - think a Swiss-army drawer of image, video, audio and text helpers rather than a single headline feature. The string nodes are the least glamorous corner of it and quietly some of the most reused.
How it works
It's Python string replacement under the hood. By default it does a plain literal swap - every occurrence of old becomes new. Flip use_regex on and old is treated as a regular expression instead, which is where it stops being trivial and starts being genuinely useful: you can match patterns, not just exact text. Strip everything in parentheses, collapse whitespace, remove a numbered suffix - all one node, if you know a little regex.
The inputs and outputs that matter
string- the text coming in. Wire it from whatever produced your prompt.old- what to find. A literal substring, or a regex pattern ifuse_regexis on.new- what to replace it with. Leave it empty to just deleteold.use_regex(default off) - the switch between "dumb literal swap" and "pattern matching". Off is what you want 90% of the time.
Output is a single STRING you feed into a CLIP Text Encode, another text node, a filename prefix - anywhere a string goes.
A small thing worth knowing: with use_regex off, characters like . ( * are matched literally, which is usually what you want. Turn regex on and those become special, so . suddenly matches any character. Don't flip the toggle unless you actually mean to write a pattern.
How to install it
Easiest path is ComfyUI Manager: open the manager, search MTB Nodes, install, restart ComfyUI. Manual install works too:
cd ComfyUI/custom_nodes
git clone https://github.com/melMass/comfy_mtb
then restart. This node is pure Python - no models, no extra pip pain, nothing to download. It'll be available the moment the pack loads.
One heads-up that applies to the whole pack: the master branch is in maintenance while the author works on the next version in the dev/0.6.0 branch. For a node this simple that changes nothing, but if you ever see a fancier MTB node in a tutorial that you can't find, the branch split is usually why.
Common issues & troubleshooting
There isn't much to break here, which is the point. The one real gotcha is the regex toggle: if your replacement "isn't working" and you've got use_regex on, your old field is being read as a pattern, and something like a+b won't match the literal text a+b. Turn regex off for literal swaps.
The other non-issue: the node replaces all occurrences, not just the first. That's usually what you want for prompt cleanup, but if you were expecting a single swap, that's why every copy changed.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| string | STRING | — | |
| old | STRING | — | |
| new | STRING | — | |
| use_regex | BOOLEAN | false | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| STRING | STRING | — |