IW ReplaceString
Find and replace for your prompts, minus the regex headache
- STRING
IW ReplaceString does one thing: find a needle inside a haystack and swap in a value. In Python terms it's haystack.replace(needle, value) - no regex, no glob, no f-string parsing. It's the workhorse of prompt templating in the iwanders/ComfyUI_nodes pack, and the defaults are set up exactly for that job.
The template pattern
Out of the box the haystack defaults to {} and the needle to %REPLACE%. So the intended flow is: write a prompt with a placeholder, and let another node supply the real value. Maybe the value comes from IW StringFromInt (your seed as text), from IW JsonPickItem (one entry from a saved JSON list), or from an LLM node like this pack's OllamaGenerateString. Whatever the source, it lands on value and gets swapped in.
Where people get burned: str.replace replaces every occurrence of the needle, not just the first. If your template mentions %REPLACE% twice, both get filled - which is usually exactly what you want for a "make this consistent across the prompt" placeholder, but surprises you if you assumed only one slot existed. There's also no escaping, so a value that itself contains %REPLACE% is just inserted as-is (Python doesn't re-scan replaced text, so no infinite loop - the author avoided that particular foot-gun by accident of using the stdlib).
The json input mode
The fourth input, input_type, defaults to verbatim but can be set to json. In json mode, the value is parsed as JSON first: if it's a string, that string is used; if it's a list, its items are joined with spaces. That last bit is the quietly useful one - it means a JSON array of tags from a file or an API becomes a space-joined tag string for your prompt in one hop. If you want commas instead of spaces you'll need to transform the list yourself, but for Danbooru-style tag prompts, spaces are correct.
What you set
- haystack - the template, multiline.
- needle - the placeholder to replace.
- value - what replaces it; accepts wired input by default.
- input_type -
verbatimorjson, as above.
Output is a single STRING, ready to feed a CLIP Text Encode or another string node.
Installing
cd ComfyUI/custom_nodes
git clone https://github.com/iwanders/ComfyUI_nodes
Restart ComfyUI, or find iwanders in ComfyUI Manager. No dependencies, no model downloads. If you're building any workflow that varies one part of a prompt while keeping the rest fixed, this node is the boring, reliable way to do it.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| haystack | STRING | {} | — |
| needle | STRING | %REPLACE% | — |
| value | STRING | — | |
| input_type | COMBO | verbatim | 2 options: json, verbatim |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| STRING | STRING | — |