Regex for Google Analytics ID
Regex Pattern
^(?:UA-\d{4,10}-\d{1,4}|G-[A-Z0-9]{10,})$Google Analytics tracking ID (UA or GA4)
Quick Answer
The regex pattern for google analytics id is `^(?:UA-\d{4,10}-\d{1,4}|G-[A-Z0-9]{10,})$`. Google Analytics tracking ID (UA or GA4). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| UA-12345678-1 | ✓ Matches |
| G-ABC1234DEF | ✓ Matches |
| UA-1234-1 | ✓ Matches |
| GA-12345 | ✗ No match |
| UA12345678 | ✗ No match |
| G-abc | ✗ No match |
Code Examples
javascript
const regex = /^(?:UA-\d{4,10}-\d{1,4}|G-[A-Z0-9]{10,})$/;
const isValid = regex.test(value);python
import re
pattern = r'^(?:UA-\d{4,10}-\d{1,4}|G-[A-Z0-9]{10,})$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^(?:UA-\d{4,10}-\d{1,4}|G-[A-Z0-9]{10,})$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^(?:UA-\d{4,10}-\d{1,4}|G-[A-Z0-9]{10,})$/', $value)) {
echo "valid";
}java
String pattern = "^(?:UA-\\d{4,10}-\\d{1,4}|G-[A-Z0-9]{10,})$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
UUID v4
UUID version 4 (randomly generated)
UUID (Any Version)
Any RFC 4122 UUID (versions 1-5)
JWT Token
JSON Web Token (header.payload.signature)
Hex String
Any-length hexadecimal string
Base64 String
Standard base64 encoded string
Alphanumeric Username
Username with letters, digits, underscores (3-16 chars)