Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Base64 = regexp.MustCompile(`^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$`)
Base64 matches Base64-encoded strings TypeScript original code: export const base64: RegExp = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
var Base64URL = regexp.MustCompile(`^[A-Za-z0-9_-]*={0,2}$`)
Base64URL pattern allows URL-safe Base64 characters with optional "=" padding (up to 2)
var Bigint = regexp.MustCompile(`^\d+n?$`)
Bigint matches big integers TypeScript original code: export const bigint: RegExp = /^\d+n?$/;
var Boolean = regexp.MustCompile(`(?i)^(true|false)$`)
Boolean matches boolean values (true/false) TypeScript original code: export const boolean: RegExp = /true|false/i;
var BrowserEmail = regexp.MustCompile(`^[a-zA-Z0-9.!#$%&'*+/=?^_` + "`" + `{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$`)
BrowserEmail matches browser email validation TypeScript original code: export const browserEmail: RegExp =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
var CIDRv4 = regexp.MustCompile(`^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$`)
CIDRv4 matches IPv4 CIDR addresses TypeScript original code: export const cidrv4: RegExp =
/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/;
var CIDRv6 = regexp.MustCompile(`^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$`)
CIDRv6 matches IPv6 CIDR addresses Updated regex to handle more IPv6 address formats with CIDR notation
var CUID = regexp.MustCompile(`^[cC][^\s-]{8,}$`)
CUID matches strings in CUID format TypeScript original code: export const cuid: RegExp = /^[cC][^\s-]{8,}$/;
var CUID2 = regexp.MustCompile(`^[0-9a-z]+$`)
CUID2 matches strings in CUID2 format TypeScript original code: export const cuid2: RegExp = /^[0-9a-z]+$/;
var Date = regexp.MustCompile(`^` + dateSource + `$`)
Date matches ISO 8601 date format (YYYY-MM-DD) TypeScript original code: export const date: RegExp = new RegExp(`^${dateSource}$`);
var DefaultDatetime = regexp.MustCompile(`^` + dateSource + `T(?:` + timeSource(nil) + `(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d))$`)
DefaultDatetime is the datetime regex with Z timezone only (no offsets by default) Updated to match TypeScript Zod 4 default behavior: only Z allowed, no offsets
var DefaultTime = regexp.MustCompile(`^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?$`)
DefaultTime is the time regex with any decimal precision Updated to support both HH:MM and HH:MM:SS formats like TypeScript Zod 4
var Domain = regexp.MustCompile(`^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
Domain matches valid domain names with TLD TypeScript original code: export const domain: RegExp = /^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/;
var Duration = regexp.MustCompile(`^P(?:(\d+W)|(\d+Y)?(\d+M)?(\d+D)?(?:T(\d+H)?(\d+M)?(\d+(?:[.,]\d+)?S)?)?)$`)
Duration matches ISO 8601-1 duration regex TypeScript original code: export const duration: RegExp =
/^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/;
Go equivalent (simplified without lookaheads): Matches either P<weeks>W or P<years>Y<months>M<days>D(T<hours>H<minutes>M<seconds>S)
var E164 = regexp.MustCompile(`^\+(?:[0-9]){6,14}[0-9]$`)
E164 matches E.164 international phone number standard TypeScript original code: export const e164: RegExp = /^\+(?:[0-9]){6,14}[0-9]$/;
var Email = regexp.MustCompile(`^[A-Za-z0-9_'+\-]+([A-Za-z0-9_'+\-]*\.[A-Za-z0-9_'+\-]+)*@[A-Za-z0-9]([A-Za-z0-9\-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9\-]*[A-Za-z0-9])?)*\.[A-Za-z]{2,}$`)
Email provides practical email validation that rejects common invalid patterns This regex rejects: - IP addresses as domains (email@123.123.123.123) - Leading/trailing dots (.email@domain.com, email.@domain.com) - Consecutive dots (email..email@domain.com) - Domains without TLD (email@domain) - Invalid IP formats (email@111.222.333.44444) Uses Go-compatible syntax without negative lookahead
var Emoji = regexp.MustCompile(`^[` +
`\x{1F600}-\x{1F64F}` +
`\x{1F300}-\x{1F5FF}` +
`\x{1F680}-\x{1F6FF}` +
`\x{1F700}-\x{1F77F}` +
`\x{1F780}-\x{1F7FF}` +
`\x{1F800}-\x{1F8FF}` +
`\x{1F900}-\x{1F9FF}` +
`\x{1FA00}-\x{1FA6F}` +
`\x{1FA70}-\x{1FAFF}` +
`\x{2600}-\x{26FF}` +
`\x{2700}-\x{27BF}` +
`\x{1F1E6}-\x{1F1FF}` +
`\x{3000}-\x{303F}` +
`\x{3200}-\x{32FF}` +
`\x{1F004}` +
`\x{1F0CF}` +
`\x{1F18E}` +
`\x{1F191}-\x{1F19A}` +
`\x{1F201}` +
`\x{1F21A}` +
`\x{1F22F}` +
`\x{1F232}-\x{1F236}` +
`\x{1F238}-\x{1F23A}` +
`\x{1F250}` +
`\x{1F251}` +
`\x{1F3FB}-\x{1F3FF}` +
`\x{200D}` +
`\x{FE0F}` +
`]+$`)
Emoji matches emoji characters (Go-compatible implementation) Note: Go's regexp doesn't support Unicode properties like \p{Extended_Pictographic} This pattern covers comprehensive emoji ranges based on Unicode standards
var ExtendedDuration = regexp.MustCompile(`^[-+]?P(?:[-+]?\d+[.,]?\d*[YMWD])*(?:T(?:[-+]?\d+[.,]?\d*[HMS])*)?$`)
ExtendedDuration implements ISO 8601-2 extensions simplified for Go TypeScript original code: export const extendedDuration: RegExp =
/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
Note: Go's regexp doesn't support lookaheads, so this is a simplified version
var GUID = regexp.MustCompile(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$`)
GUID matches any UUID-like identifier with 8-4-4-4-12 hex pattern TypeScript original code: export const guid: RegExp = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/;
var HTTPUrl = regexp.MustCompile(`^https?://[^\s/$.?#].[^\s]*$`)
HTTPUrl matches URLs starting with http or https specifically
var Hostname = regexp.MustCompile(`^([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+$`)
Hostname matches DNS hostnames TypeScript original code: export const hostname: RegExp = /^([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+$/;
var Html5Email = regexp.MustCompile(`^[a-zA-Z0-9.!#$%&'*+/=?^_` + "`" + `{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$`)
Html5Email matches HTML5 input[type=email] validation implemented by browsers TypeScript original code: export const html5Email: RegExp =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
var IP = regexp.MustCompile(`((?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]))|((?:(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|(?:[0-9a-fA-F]{1,4})?::(?:[0-9a-fA-F]{1,4}:?){0,6}))`)
IP matches any IP address (IPv4 or IPv6) TypeScript original code: export const ip: RegExp = new RegExp(`(${ipv4.source})|(${ipv6.source})`);
var IPv4 = regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$`)
IPv4 matches IPv4 addresses TypeScript original code: export const ipv4: RegExp =
/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
var IPv6 = regexp.MustCompile(`^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$`)
IPv6 matches IPv6 addresses – comprehensive pattern covering multiple compressed forms. TypeScript original code: export const ipv6: RegExp =
/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})$/;
var Integer = regexp.MustCompile(`^\d+$`)
Integer matches integers TypeScript original code: export const integer: RegExp = /^\d+$/;
var JSONString = regexp.MustCompile(`^[\s\S]*$`)
JSONString matches any valid JSON string format Note: This is a simplistic pattern; actual validation should be done at runtime
var KSUID = regexp.MustCompile(`^[A-Za-z0-9]{27}$`)
KSUID matches strings in KSUID format TypeScript original code: export const ksuid: RegExp = /^[A-Za-z0-9]{27}$/;
var Lowercase = regexp.MustCompile(`^[^A-Z]*$`)
Lowercase matches strings with no uppercase letters TypeScript original code: export const lowercase: RegExp = /^[^A-Z]*$/;
var NanoID = regexp.MustCompile(`^[a-zA-Z0-9_-]{21}$`)
NanoID matches strings in NanoID format TypeScript original code: export const nanoid: RegExp = /^[a-zA-Z0-9_-]{21}$/;
var Null = regexp.MustCompile(`(?i)^null$`)
Null matches null values TypeScript original code: const _null: RegExp = /null/i; export { _null as null };
var Number = regexp.MustCompile(`^-?\d+(?:\.\d+)?`)
Number matches numbers including decimals and negative numbers TypeScript original code: export const number: RegExp = /^-?\d+(?:\.\d+)?/i;
var Rfc5322Email = regexp.MustCompile(`^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`)
Rfc5322Email matches the classic emailregex.com regex for RFC 5322-compliant emails TypeScript original code: export const rfc5322Email =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var String = regexp.MustCompile(`^[\s\S]*$`)
String matches any string with no length restrictions TypeScript original code:
export const string = (params?: { minimum?: number | undefined; maximum?: number | undefined }): RegExp => {
const regex = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : `[\\s\\S]*`;
return new RegExp(`^${regex}$`);
};
var ULID = regexp.MustCompile(`^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$`)
ULID matches strings in ULID format TypeScript original code: export const ulid: RegExp = /^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/;
var URL = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9+.-]*://[^\s/$.?#].[^\s]*$`)
URL matches valid URLs with proper format validation Supports common protocols: http, https, ftp, ftps, ws, wss, file, etc. Based on RFC 3986 with practical adaptations for common use cases
var UUID = regexp.MustCompile(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$`)
UUID matches RFC 4122 UUID with all versions supported when no version is specified TypeScript original code:
export const uuid = (version?: number | undefined): RegExp => {
if (!version)
return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$/;
return new RegExp(
`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`
);
};
var UUID4 = regexp.MustCompile(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`)
UUID4 matches version 4 UUIDs TypeScript original code: export const uuid4: RegExp = uuid(4);
var UUID6 = regexp.MustCompile(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-6[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`)
UUID6 matches version 6 UUIDs TypeScript original code: export const uuid6: RegExp = uuid(6);
var UUID7 = regexp.MustCompile(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-7[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`)
UUID7 matches version 7 UUIDs TypeScript original code: export const uuid7: RegExp = uuid(7);
var Undefined = regexp.MustCompile(`(?i)^undefined$`)
Undefined matches undefined values TypeScript original code: const _undefined: RegExp = /undefined/i; export { _undefined as undefined };
var UnicodeEmail = regexp.MustCompile(`^[^\s@"]{1,64}@[^\s@]{1,255}$`)
UnicodeEmail matches a loose regex that allows Unicode characters and enforces length limits TypeScript original code: export const unicodeEmail = /^[^\s@"]{1,64}@[^\s@]{1,255}$/u; Note: Go's regexp doesn't support full Unicode properties, so this regex is simplified
var Uppercase = regexp.MustCompile(`^[^a-z]*$`)
Uppercase matches strings with no lowercase letters TypeScript original code: export const uppercase: RegExp = /^[^a-z]*$/;
var XID = regexp.MustCompile(`^[0-9a-vA-V]{20}$`)
XID matches strings in XID format TypeScript original code: export const xid: RegExp = /^[0-9a-vA-V]{20}$/;
Functions ¶
func Datetime ¶
func Datetime(options DatetimeOptions) *regexp.Regexp
Datetime returns a regex for matching ISO 8601 datetime format TypeScript original code:
export function datetime(args: {
precision?: number | null;
offset?: boolean;
local?: boolean;
}): RegExp {
let regex = `${dateSource}T${timeSource(args)}`;
const opts: string[] = [];
opts.push(args.local ? `Z?` : `Z`);
if (args.offset) opts.push(`([+-]\\d{2}:?\\d{2})`);
regex = `${regex}(${opts.join("|")})`;
return new RegExp(`^${regex}$`);
}
func StringRegex ¶
StringRegex returns a regex matching strings with optional min/max length limits
func Time ¶
func Time(opts TimeOptions) *regexp.Regexp
Time returns a regex for matching ISO 8601 time format TypeScript original code:
export function time(args: {
precision?: number | null;
}): RegExp {
return new RegExp(`^${timeSource(args)}$`);
}
func UUIDForVersion ¶
UUIDForVersion returns a regex matching a specific UUID version (1-8) TypeScript original code:
export const uuid = (version?: number | undefined): RegExp => {
if (!version)
return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$/;
return new RegExp(
`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`
);
};
Types ¶
type DatetimeOptions ¶
type DatetimeOptions struct {
// Precision specifies number of decimal places for seconds
// If nil, matches any number of decimal places
// If 0 or negative, no decimal places allowed
Precision *int
// Offset if true, allows timezone offsets like +01:00
Offset bool
// Local if true, makes the 'Z' timezone marker optional
Local bool
}
DatetimeOptions defines parameters for datetime regex pattern TypeScript original code:
export function datetime(args: {
precision?: number | null;
offset?: boolean;
local?: boolean;
}): RegExp
type TimeOptions ¶
type TimeOptions struct {
// Precision specifies number of decimal places for seconds
// If nil, matches any number of decimal places
// If 0 or negative, no decimal places allowed
Precision *int
}
TimeOptions defines parameters for time regex pattern TypeScript original code:
function timeSource(args: { precision?: number | null }) {
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
if (args.precision) {
regex = `${regex}\\.\\d{${args.precision}}`;
} else if (args.precision == null) {
regex = `${regex}(\\.\\d+)?`;
}
return regex;
}