Building regex patterns with user input? This guide shows you how to use the Regex Escape API to safely escape special characters, preventing regex injection and ensuring your patterns work correctly with literal strings.
Why Escape Regex Characters?
Regular expressions use special characters like ., *, ?, and + with specific meanings. If you want to match these characters literally (for example, searching for "file.txt"), you need to escape them.
Without escaping, searching for "price: $10.00" would behave unexpectedly because $ and . are regex metacharacters.
Special Characters in Regex
These characters have special meaning in regex and need escaping for literal matching:
.- Matches any character*- Zero or more of previous+- One or more of previous?- Zero or one of previous^- Start of string$- End of string|- Alternation (or)[ ]- Character class( )- Grouping{ }- Quantifier\- Escape character
Using the Regex Escape API
TinyFn provides a simple endpoint for escaping regex characters:
GET https://api.tinyfn.io/v1/regex/escape
Headers: X-API-Key: your-api-key
{
"original": "file.txt (v1.0)",
"escaped": "file\\.txt \\(v1\\.0\\)",
"characters_escaped": 4
}
Parameters
| Parameter | Type | Description |
|---|---|---|
text |
string | The text to escape for regex use (required) |
Code Examples
JavaScript / Node.js
// Escape user search term for regex
const userSearch = "C++ (Programming)";
const response = await fetch(
'https://api.tinyfn.io/v1/regex/escape?' + new URLSearchParams({
text: userSearch
}),
{ headers: { 'X-API-Key': 'your-api-key' } }
);
const result = await response.json();
console.log(result.escaped); // "C\\+\\+ \\(Programming\\)"
// Now safe to use in regex pattern
const pattern = new RegExp(result.escaped, 'gi');
Python
import requests
import re
# Escape user input before using in regex
user_input = "What is 2+2?"
response = requests.get(
'https://api.tinyfn.io/v1/regex/escape',
params={'text': user_input},
headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['escaped']) # "What is 2\\+2\\?"
# Safe to use in pattern
pattern = re.compile(result['escaped'])
cURL
curl "https://api.tinyfn.io/v1/regex/escape?text=price%3A%20%2410.00" \
-H "X-API-Key: your-api-key"
Common Use Cases
- Search Features: Allow users to search for literal text including special characters
- Dynamic Patterns: Build regex patterns from variable input
- Code Generation: Generate regex patterns programmatically
- Find and Replace: Replace text containing special characters
- Security: Prevent regex injection from untrusted input
Best Practices
- Always escape user input: Never use untrusted input directly in regex patterns
- Escape before concatenation: Escape the dynamic parts, then combine with your pattern
- Test edge cases: Test with strings containing multiple special characters
- Consider alternatives: Sometimes string indexOf/includes is simpler than regex
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 Escape API
Get your free API key and safely build dynamic regex patterns.
Get Free API Key