API Key Pattern Regex Pattern
Detects potential API keys and tokens in source code. Used by security tools to prevent accidental secret exposure.
Live Regex Tester
Pattern Breakdown
Code Examples
JavaScript
const regex = /(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?/gi;
const test = "api_key="sk_live_abc123def456ghi789"";
console.log(regex.test(test)); // true
// Extract matches
const matches = test.match(regex);
console.log(matches);Python
import re
pattern = r'(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?'
test = "api_key="sk_live_abc123def456ghi789""
match = re.findall(pattern, test, re.IGNORECASE)
print(match) # Found!Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?`)
fmt.Println(re.MatchString("api_key="sk_live_abc123def456ghi789"")) // true
}Common Use Cases
Match Examples
| Input | Result |
|---|---|
| api_key="sk_live_abc123def456ghi789" | Match |
| name=John | No Match |
About the API Key Pattern Regex
Detects potential API keys and tokens in source code. Used by security tools to prevent accidental secret exposure.
Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The api key pattern pattern is classified as advanced difficulty in the security & auth category. It works in all major programming languages.
When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.
More Security & Auth Patterns
Need More Regex Patterns?
Browse our complete library of 100+ regex patterns with interactive testers.
Frequently Asked Questions
What is the API Key Pattern regex pattern?
Detects potential API keys and tokens in source code. Used by security tools to prevent accidental secret exposure.
How do I use the API Key Pattern regex?
Use the pattern /(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["']?([a-zA-Z0-9_-]{20,})["']?/gi in your code. In JavaScript: new RegExp('(?:api[_-]?key|apikey|token)[\s]*[=:][\s]*["\']?([a-zA-Z0-9_-]{20,})["\']?', 'gi'). Test it above with your own input.
What does this API Key Pattern regex match?
This pattern matches: "api_key="sk_live_abc123def456ghi789"". It does NOT match: "name=John". Secret scanning, code review, security auditing.
Is the API Key Pattern regex beginner-friendly?
This pattern is rated Advanced. It uses advanced features like lookaheads, backreferences, or complex alternation.
What languages support the API Key Pattern regex?
This pattern works in all major programming languages including JavaScript, Python, Java, C#, Go, Ruby, PHP, and more. Syntax may vary slightly between regex engines.
Can I modify the API Key Pattern regex for my use case?
Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.
Related Tools
JWT Token Regex
Intermediate Security & Auth pattern
UUID (v4) Regex
Intermediate Security & Auth pattern
AWS Access Key Regex
Intermediate Security & Auth pattern
Base64 Encoded String Regex
Beginner Security & Auth pattern
MD5 Hash Regex
Beginner Security & Auth pattern
SHA-256 Hash Regex
Beginner Security & Auth pattern