Need to perform complex find and replace operations using regular expressions? This guide shows you how to use the Regex Replace API to transform text with pattern matching, capture groups, and flexible replacement options.
What is Regex Replace?
Regex replace uses regular expression patterns to find text and replace it with a specified substitution. Unlike simple find and replace, regex allows you to match complex patterns and use captured portions of the match in your replacement.
For example, you can swap the order of names, reformat dates, or clean up inconsistent data formats.
Using Capture Groups
Capture groups are portions of your pattern enclosed in parentheses. You can reference these in your replacement using $1, $2, etc.
- $0 or $&: The entire matched string
- $1, $2, $3...: The first, second, third capture group
- $$: A literal dollar sign
(\w+), (\w+) matches "Smith, John". Replacement $2 $1 produces "John Smith".
Using the Regex Replace API
TinyFn provides a powerful endpoint for regex replacement:
POST https://api.tinyfn.io/v1/regex/replace
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"text": "Hello World, Hello Universe",
"pattern": "Hello (\\w+)",
"replacement": "Hi $1"
}
{
"result": "Hi World, Hi Universe",
"original": "Hello World, Hello Universe",
"matches_replaced": 2,
"pattern": "Hello (\\w+)",
"flags": "g"
}
Parameters
| Parameter | Type | Description |
|---|---|---|
text |
string | The input text to perform replacement on (required) |
pattern |
string | Regular expression pattern to match (required) |
replacement |
string | Replacement string with optional capture group references (required) |
flags |
string | Regex flags: g (global), i (case-insensitive), m (multiline). Default: "g" |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/regex/replace',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'Date: 2024-01-15, Date: 2024-02-20',
pattern: '(\\d{4})-(\\d{2})-(\\d{2})',
replacement: '$2/$3/$1' // Convert to MM/DD/YYYY
})
}
);
const result = await response.json();
console.log(result.result); // "Date: 01/15/2024, Date: 02/20/2024"
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/regex/replace',
json={
'text': 'Contact: john.doe@email.com',
'pattern': r'(\w+)\.(\w+)@(\w+)\.com',
'replacement': r'$1_$2@$3.org'
},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['result']) # "Contact: john_doe@email.org"
cURL
curl -X POST "https://api.tinyfn.io/v1/regex/replace" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"text":"HELLO world","pattern":"hello","replacement":"Hi","flags":"gi"}'
Common Use Cases
- Data Cleaning: Normalize inconsistent data formats
- Date Reformatting: Convert between date formats
- Name Manipulation: Swap first/last name order
- URL Rewriting: Transform URL structures
- Masking Sensitive Data: Redact portions of sensitive information
Best Practices
- Test patterns carefully: Regex can have unintended matches
- Escape special characters: Literal characters like . or * need escaping
- Use non-greedy quantifiers: Use *? or +? to avoid over-matching
- Consider edge cases: What happens with empty strings or no matches?
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-regex": {
"url": "https://api.tinyfn.io/mcp/regex/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all regex tools available via MCP in our Regex MCP Tools for AI Agents guide.
Try the Regex Replace API
Get your free API key and start transforming text with powerful patterns.
Get Free API Key