regex-repo
v5.0.1
Published
A collection of useful regular expressions.
Downloads
109
Maintainers
Readme
regex-repo
A a collection of useful regular expressions. Refer to the regex reference below for a list of the provided REs.
Installation
npm i @liquid-labs/regex-repo
Supports both CJS and ESM packages.
Usage
import { emailRe } from '@liquid-labs/regex-repo' // ES6
// const { emailRe } = require('@liquid-labs/regex-repo') // cjs
const verified = emailRe.test(userInput)
Regex reference
Each regular expression listed below is paired with an embeddable string named xxxString
. E.g., rgbRe
is paired with rgbReString
. Each Re will only match strings that are the given type and nothing else. I.e., the RE begins with '^' and ends with '$'. The xxxReString
can be used for partial matches, matchAll
s, and used as part of larger expressions. E.g., to find all unique CSS RGB colors used in a style sheet, you might do something like:
import { rgbReString } from '@liquid-labs/regex-repo'
const allColors = cssContent
.matchAll(new RegExp(`[ :](${rgbReString})[; }]`, 'g'))
.map((match) => match[1]) // extract the capture group
.filter((v, i, arr) => i === arr.indexOf(v)) // filter non-unique items
.sort()
API generated with dmd-readme-api.
- Constants:
- AWS
awsS3BucketNameRe
: Matches (most) valid S3 bucket name.awsS3TaBucketNameRe
: Matches (most) S3 Transfer Acceleration compatible S3 bucket name.
- Contacts
- CSS
cssColor3Re
: Matches CSS3 'hex, rgb, rgba, hsl, and predefined colors.cssColorRe
: Matches CSS4 'hex, rgb, rgba, hsl, and predefined colors.cssPreColors1Re
: Matches CSS1 predefined color names.cssPreColors2Re
: Matches CSS2 predefined color names.cssPreColors3Re
: Matches CSS3 predefined color names.cssPreColorsRe
: Matches CSS4 predefined color names.hexColorAlphaRe
: Matches hex specified RGBA colors with an alpha channel.hexColorNoAlphaRe
: Matches hex specified RGB colors with no alpha channel.hsl3Re
: Matches CSS3 'hsl(...) and hsla(...) deg and percent notation.hslRe
: Matches CSS4 'hsl(...) and hsla(...) deg, grad, rad, turn and percent notation.rgbaFuncRe
: Matches CSS3 'rgba(...) using '0...255 and percent (integer) notation.rgbFuncRe
: Matches CSS1 'rgb(...) using '0...255 and percent (integer) notation.rgbRe
: Matches CSS4 'rgb(...) and rgba(...) functios using '0...255 and percent (float) notation.
- CSS numbers
zeroTo100FloatPercentRe
: Matches a 0 to 100% float as used in CSS color specifications.zeroTo100PercentRe
: Matches a 0 to 100% integer as used in CSS color specifications.zeroTo1FloatRe
: Matches a 0 to 1 float as used in CSS color specifications.zeroTo255FloatRe
: Matches a 0 to 255 float as used in CSS color specifications.zeroTo255Re
: Matches a 0 to 255 integer as used in CSS color specifications.zeroTo360FloatRe
: Matches a 0 to 360 float as used in CSS color specifications.zeroTo360Re
: Matches a 0 to 360 integer as used in CSS color specifications.
- Date time
intlDateRe
: Matches an international style 'YYYY/MM/DD' string.iso8601DateRe
: Matches an ISO 8601 date time like '20240101T1212Z.iso8601DateReString
: Matches the time designation portion of an ISO 8601 date+time.iso8601DateTimeRe
: Matches an ISO 8601 requiring both date and time components.iso8601DayReString
: An RE ready string that matches the day designation portion of an ISO 8601 date+time.militaryTimeRe
: Matches military time style 'HHMM' string.rfc2822DateRe
: Matches an RFC 2822 style date like 'Mon, 6 Jan 1992 12:12 UTC'.rfc2822DayReString
: An RE ready string that matches the day designation portion of an RFC 2822 date+time.rfc2822TimeReString
: An RE ready string that matches the time designation portion of an RFC 2822 date+time.timeRe
: Matches a twelve hour time designation, requires AM or PM designation.timezoneReString
: An RE ready string that matches a general timezone designation; compliant with RFC 2822 timezone portion.twentyFourHourTimeRe
: Matches a twenty-four hour time designationAllows optional leading 0 in hour.usDateRe
: Matches a US style 'MM/DD/YYYY' string.
- Domain names
domainLabelRe
: Matches a non-tld domain label.fqDomainNameRe
: Matches fully qualified domain name (one or more subdomains + TLD).localhostRe
: Matches any representation of localhost; the special name, IPV4 loopbacks, or IPV6 loopbacks.tldNameRe
: Matches a Top Level Domain (TLD).
- Identifiers
- Javascript
jsReservedWordRe
: Matches a JS resereved word.jsVariableRe
: Matches a valid JS variable name.
- Network
ipAddressRe
: Matches a string in IP address format.ipHostRe
: Matches a valid, non-localhost IP address.ipV6Re
: Matches a string in IPV6 format.ipVFutureRe
: Matches potential future IP protocols.
- NPM
npmPackageNameRe
: Matches an NPM package name.
- Numbers
floatRe
: Matches a float in either plan or scientific format.integerRe
: Matches an integer.plainFloatRe
: Matches a plain (non-scientific notation) float.scientificFloatRe
: Matches a scientific notation float.
- URL
commonUrlRe
: Matches any of the "common" web URL types: 'mailto', 'http/https', 'ftp', and 'file'.fileUrlRe
: Matches a valid 'file' URL.ftpUrlRe
: Matches a valid 'ftp' URL.httpUrlRe
: Matches a valid 'http/https' URL.mailtoUrlRe
: Matches a valid 'mailto:' URL.urlRe
: Matches a valid, generic URL.
- AWS
awsS3BucketNameRe
↱source code ⇧AWS index | global index
Matches (most) valid S3 bucket name. Note awsS3BucketNameReString
cannot be used for partial matches. Does not
enforce 63 character limit.
awsS3TaBucketNameRe
↱source code ⇧AWS index | global index
Matches (most) S3 Transfer Acceleration compatible S3 bucket name. Note awsS3TaBucketNameReString
cannot be used
for partial matches.
emailRe
↱source code ⇧Contacts index | global index
Match most valid emails. Provides matching groups 1 (user name) and 2 (domain). When using the partial string to create a Re, you must use the 'u' flag.
usPhoneRe
↱source code ⇧Contacts index | global index
Matches US phone numbers with optional country code and area code.
zipCodeRe
↱source code ⇧Contacts index | global index
Matches 5 or 9 digit US zip codes.
cssColor3Re
↱source code ⇧CSS index | global index
Matches CSS3 'hex, rgb, rgba, hsl, and predefined colors.
cssColorRe
↱source code ⇧CSS index | global index
Matches CSS4 'hex, rgb, rgba, hsl, and predefined colors.
cssPreColors1Re
↱source code ⇧CSS index | global index
Matches CSS1 predefined color names.
cssPreColors2Re
↱source code ⇧CSS index | global index
Matches CSS2 predefined color names.
cssPreColors3Re
↱source code ⇧CSS index | global index
Matches CSS3 predefined color names.
cssPreColorsRe
↱source code ⇧CSS index | global index
Matches CSS4 predefined color names.
hexColorAlphaRe
↱source code ⇧CSS index | global index
Matches hex specified RGBA colors with an alpha channel.
hexColorNoAlphaRe
↱source code ⇧CSS index | global index
Matches hex specified RGB colors with no alpha channel.
hsl3Re
↱source code ⇧CSS index | global index
Matches CSS3 'hsl(...) and hsla(...) deg and percent notation.
hslRe
↱source code ⇧CSS index | global index
Matches CSS4 'hsl(...) and hsla(...) deg, grad, rad, turn and percent notation.
rgbaFuncRe
↱source code ⇧CSS index | global index
Matches CSS3 'rgba(...) using '0...255 and percent (integer) notation.
rgbFuncRe
↱source code ⇧CSS index | global index
Matches CSS1 'rgb(...) using '0...255 and percent (integer) notation.
rgbRe
↱source code ⇧CSS index | global index
Matches CSS4 'rgb(...) and rgba(...) functios using '0...255 and percent (float) notation.
zeroTo100FloatPercentRe
↱source code ⇧CSS numbers index | global index
Matches a 0 to 100% float as used in CSS color specifications.
zeroTo100PercentRe
↱source code ⇧CSS numbers index | global index
Matches a 0 to 100% integer as used in CSS color specifications.
zeroTo1FloatRe
↱source code ⇧CSS numbers index | global index
Matches a 0 to 1 float as used in CSS color specifications.
zeroTo255FloatRe
↱source code ⇧CSS numbers index | global index
Matches a 0 to 255 float as used in CSS color specifications.
zeroTo255Re
↱source code ⇧CSS numbers index | global index
Matches a 0 to 255 integer as used in CSS color specifications.
zeroTo360FloatRe
↱source code ⇧CSS numbers index | global index
Matches a 0 to 360 float as used in CSS color specifications.
zeroTo360Re
↱source code ⇧CSS numbers index | global index
Matches a 0 to 360 integer as used in CSS color specifications.
intlDateRe
↱source code ⇧Date time index | global index
Matches an international style 'YYYY/MM/DD' string. Accepts separators '.', '/', '-'. Will except 1 or 2 digits for month and day and 1-4 digits for the year. Also accepts a + or - before the year. Provides capture groups 1 (BCE/CE indicator), 2 (year), 3 (month), 4 (day).
iso8601DateRe
↱source code ⇧Date time index | global index
Matches an ISO 8601 date time like '20240101T1212Z. Provides matching groups 1 (year), 3 (month), and 4 (day of month), 5 (week of year), 6 (day of week date), and 7 (ordinal or Julian date), 8 (special end of day time), 10 (hour), 11 (decimal fraction of hour), 13 (minute), 14 (decimal fraction of minute), 15 (seconds), 16 (decimal fraction of a second), and 17 (timezone designation). (Groups 2, 11, and 13 are internal back references.)
iso8601DateReString
↱source code ⇧Date time index | global index
Matches the time designation portion of an ISO 8601 date+time. Provides matching groups 1 (special end of day time), 3 (hours), 3 (fraction of hour), 5 (minutes), 6 (fraction of minute), 7 (seconds), and 8 (fraction of seconds).
iso8601DateTimeRe
↱source code ⇧Date time index | global index
Matches an ISO 8601 requiring both date and time components. See iso8601DateRe for matching groups.
iso8601DayReString
↱source code ⇧Date time index | global index
An RE ready string that matches the day designation portion of an ISO 8601 date+time. Provides matching groups 1 ( year), 3 (month), and 4 (day of month), 5 (week of year), 6 (day of week date), and 7 (ordinal or Julian date).
militaryTimeRe
↱source code ⇧Date time index | global index
Matches military time style 'HHMM' string. Provides capture groups 1 (special 2400 time), 2 (hour), and 3 (minutes).
rfc2822DateRe
↱source code ⇧Date time index | global index
Matches an RFC 2822 style date like 'Mon, 6 Jan 1992 12:12 UTC'. Provides matching groups 1 (day of week), 2 (day of month), 3 (month name), and 4 (year), 5 (hour), 6 (min), 7 (second), and 8 (time zone).
rfc2822DayReString
↱source code ⇧Date time index | global index
An RE ready string that matches the day designation portion of an RFC 2822 date+time. Provides matching groups 1 (day of week name), 2 (day of month), 3 (month name), 4 (year).
rfc2822TimeReString
↱source code ⇧Date time index | global index
An RE ready string that matches the time designation portion of an RFC 2822 date+time. Provides matching groups 1 (hour), 2 (minutes), 3 (seconds), and 4 (timezone).
timeRe
↱source code ⇧Date time index | global index
Matches a twelve hour time designation, requires AM or PM designation. Allows optional leading 0 in hour. Provides matching groups 1 (hour), 2 (minutes), 3 (seconds, without decimal fractions), 4 (decimal fraction seconds), and 5 ( AM/PM indicator).
timezoneReString
↱source code ⇧Date time index | global index
An RE ready string that matches a general timezone designation; compliant with RFC 2822 timezone portion. Provides matching groups 1 (timezone).
twentyFourHourTimeRe
↱source code ⇧Date time index | global index
Matches a twenty-four hour time designationAllows optional leading 0 in hour. Provides matching groups 1 (special 24:00 designation with optional seconds), 2 (hour), 3 (minutes), 4 (seconds, without decimal fractions), 5 (decimal fraction seconds).
usDateRe
↱source code ⇧Date time index | global index
Matches a US style 'MM/DD/YYYY' string. Accepts separators '.', '/', '-'. Will except 1 or 2 digits for month and day and 1-4 digits for the year. Also accepts a + or - before the year. Provides capture groups 1 (month), 2 (day of month), 3 (BCE/CE indicator), and 4 (year).
domainLabelRe
↱source code ⇧Domain names index | global index
Matches a non-tld domain label. Enforces the 63 byte domain label limit for non-international (all ASCII) labels. See domain name rules. When using the partial string to create a Re, you must use the 'u' or 'v' flag.
fqDomainNameRe
↱source code ⇧Domain names index | global index
Matches fully qualified domain name (one or more subdomains + TLD). Partially enforces the 255 byte FQ domain name limit, but this is only valid for non-international (all ASCII) domain names because we can only count characters. When using the partial string to create a Re, you must use the 'u' or 'v' flag.
localhostRe
↱source code ⇧Domain names index | global index
Matches any representation of localhost; the special name, IPV4 loopbacks, or IPV6 loopbacks.
tldNameRe
↱source code ⇧Domain names index | global index
Matches a Top Level Domain (TLD). See domain name rules. When using the partial string to create a Re, you must use the 'u' or 'v' flag.
einRe
↱source code ⇧Identifiers index | global index
Matches a valid EIN number.
ssnRe
↱source code ⇧Identifiers index | global index
Matches a valid SSN. Provides 3 matching groups, 1 (area number), 2 (group number), and 3 (serial number).
uuidRe
↱source code ⇧Identifiers index | global index
Matches a UUID.
jsReservedWordRe
↱source code ⇧Javascript index | global index
Matches a JS resereved word.
jsVariableRe
↱source code ⇧Javascript index | global index
Matches a valid JS variable name.
ipAddressRe
↱source code ⇧Network index | global index
Matches a string in IP address format. Use 'ipHostRe' to match actually valid IP addresses.
ipHostRe
↱source code ⇧Network index | global index
Matches a valid, non-localhost IP address.
ipV6Re
↱source code ⇧Network index | global index
Matches a string in IPV6 format.
ipVFutureRe
↱source code ⇧Network index | global index
Matches potential future IP protocols.
npmPackageNameRe
↱source code ⇧NPM index | global index
Matches an NPM package name. Provides matching groups 1 (org name, if any) and 2 (package basename).
floatRe
↱source code ⇧Numbers index | global index
Matches a float in either plan or scientific format.
integerRe
↱source code ⇧Numbers index | global index
Matches an integer.
plainFloatRe
↱source code ⇧Numbers index | global index
Matches a plain (non-scientific notation) float.
scientificFloatRe
↱source code ⇧Numbers index | global index
Matches a scientific notation float.
commonUrlRe
↱source code ⇧URL index | global index
Matches any of the "common" web URL types: 'mailto', 'http/https', 'ftp', and 'file'. You must use the either the 'u' or 'v' flag when using the Re string.
fileUrlRe
↱source code ⇧URL index | global index
Matches a valid 'file' URL. Provides capture groups 1 (host), 2 (port), 3 (path). You must use the either the 'u' or 'v' flag when using the Re string.
ftpUrlRe
↱source code ⇧URL index | global index
Matches a valid 'ftp' URL. Provides capture groups 1 (username), 2 (user password), 3 (host or IP), 4 (port), 5 (path). You must use the either the 'u' or 'v' flag when using the Re string.
httpUrlRe
↱source code ⇧URL index | global index
Matches a valid 'http/https' URL. Provides capture groups 1 (protocol), 2 (username), 3 (user password), 4 (host or IP), 5 (port), 6 (path), 7 (query string), and 8 (fragment). You must use the either the 'u' or 'v' flag when using the Re string.
mailtoUrlRe
↱source code ⇧URL index | global index
Matches a valid 'mailto:' URL. Provides a single capture group, 1 (email address).' You must use the either the 'u' or 'v' flag when using the Re string.
urlRe
↱source code ⇧URL index | global index
Matches a valid, generic URL. Provides capture groups 1 (schema), 2 (server/authority), 3 (path), 4 (query part), 5 (
intra-page link/fragment.) Note, a URL always has scheme, and at a minimum a server/authority or path, and may have
both. The query and fragment components are always optional. For general usage, you might want to use the more
specific Res for specific protocols or the commonUrlRe
.
Domain name rules
Unfortunately, there isn't clear consensus on what is allowed in a subdomain vs a top level domain (TLD); referred to collectively as 'domain labels'. So, here are the rules we follow:
- Domain labels may no more than 63 bytes in length.
- Labels are composed of alpha-numeric characters (a-z, 0-9, and any non-ASCII Unicode character) and hyphens ('-'), except:
- the label may not begin or end with a hyphen,
- may not consist of a single digit,
- the label must not have consecutive hyphens in the 3rd and 4th position. E.g. 'xy--z' is invalid.[^1]
- TLDs must be at least two bytes (two ASCII characters or a single Unicode character) and may not be composed only of digits.
- A fully qualified domain is limited to 255 bytes in total.
[^1]: The DNS protocol only allows a-z, 0-9, and '-' in domain labels. International domains are encoded as special 'xn--' domains. E.g., 'कॉम"' is encoded as 'xn--11b4c3d'. This is why hyphens in the third and fourth position are restricted. So, while 'xn--11b4c3d' is a valid domain, you can't register such domains directly. You would register the international domain and it's translated to an 'xn--' domain in the background.