JSON TransformString
replace
Replace the first occurrence of a substring
replace
Replaces the first occurrence of pattern with replacement in a string. To replace every occurrence, use replaceAll.
Options
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
pattern | string | Yes | — | Literal substring to find. |
replacement | string | Yes | — | Substring to insert in place of the match. |
Example
Input
"foo bar foo"Operation
{ "type": "replace", "options": { "pattern": "foo", "replacement": "qux" } }Output
"qux bar foo"In a Circuit Step
{
"name": "rewrite_first_token",
"description": "Replace the first occurrence of `foo` in the input",
"function": "circuit.core.transform.json",
"input": {
"source": "${{ input.text }}",
"operations": [
{ "type": "replace", "options": { "pattern": "foo", "replacement": "qux" } }
]
},
"outputs": [
{
"name": "rewritten_text",
"description": "Input text with the first `foo` replaced by `qux`",
"value": "${{ rewrite_first_token.output.transformed_result }}"
}
]
}Notes & gotchas
- Errors with
replace: expected stringif the input is not a string. patternis treated as a literal substring, not a regex.- If
patternis not found, the original string is returned unchanged. - For all-occurrences semantics, use
replaceAll.