URL Regex Pattern API: Validate URLs and Web Addresses

Need to validate URLs in your application? This guide covers the URL Regex Pattern API, which provides tested patterns for validating web addresses including protocols, domains, paths, query strings, and fragments.

Understanding URL Structure

A URL consists of several components: protocol, domain, port, path, query string, and fragment. For example:

https://example.com:8080/path/to/page?query=value#section
|_____|  |_________|_____|____________|___________|_______|
protocol  domain   port    path        query      fragment

Pattern Types

The API provides patterns for different URL validation needs:

  • Web: HTTP/HTTPS URLs only (most common)
  • Any Protocol: Any valid protocol (ftp://, mailto:, etc.)
  • Strict: Full URL validation with all components
  • Loose: Allows URLs without protocol (example.com)
Note: For most web applications, the "web" pattern (HTTP/HTTPS only) is recommended for security reasons.

Using the URL Regex API

TinyFn provides pre-built URL validation patterns:

API Request
GET https://api.tinyfn.io/v1/regex/patterns/url
Headers: X-API-Key: your-api-key
Response
{
  "pattern": "^https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)$",
  "type": "web",
  "description": "Standard HTTP/HTTPS URL validation pattern",
  "flags": "i",
  "examples": {
    "valid": ["https://example.com", "http://sub.domain.org/path?q=1"],
    "invalid": ["ftp://files.com", "not-a-url", "javascript:alert(1)"]
  }
}

Parameters

Parameter Type Description
type string Pattern type: web, any_protocol, strict, loose. Default: web
require_protocol boolean Require protocol prefix (http://). Default: true
anchored boolean Include ^ and $ anchors. Default: true

Code Examples

JavaScript / Node.js

// Get the URL pattern
const response = await fetch(
  'https://api.tinyfn.io/v1/regex/patterns/url?type=web',
  { headers: { 'X-API-Key': 'your-api-key' } }
);
const { pattern, flags } = await response.json();

// Validate URLs
const urlRegex = new RegExp(pattern, flags);
console.log(urlRegex.test('https://example.com')); // true
console.log(urlRegex.test('javascript:alert(1)')); // false (blocked!)

Python

import requests
import re

response = requests.get(
    'https://api.tinyfn.io/v1/regex/patterns/url',
    params={'type': 'strict'},
    headers={'X-API-Key': 'your-api-key'}
)
result = response.json()

url_pattern = re.compile(result['pattern'], re.IGNORECASE)
urls_to_test = [
    'https://example.com/path?q=1#section',
    'http://localhost:3000',
    'not a url'
]
for url in urls_to_test:
    print(f"{url}: {bool(url_pattern.match(url))}")

cURL

curl "https://api.tinyfn.io/v1/regex/patterns/url?type=loose&require_protocol=false" \
  -H "X-API-Key: your-api-key"

Common Use Cases

  • Form Validation: Validate URL inputs in forms
  • Link Extraction: Find URLs in text content
  • Security: Block dangerous protocols like javascript:
  • Content Filtering: Validate user-submitted links
  • API Validation: Validate webhook or callback URLs

Best Practices

  1. Use web pattern for user input: Restrict to HTTP/HTTPS for security
  2. Validate before redirecting: Always validate URLs before redirecting users
  3. Consider URL parsing: For complex validation, consider using URL parsers instead
  4. Handle internationalized domains: Some patterns may not support IDN domains

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 URL Regex Pattern API

Get your free API key and start validating URLs securely.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key