Regex

Check Regex Patterns in JavaScript with helping-js.

🚀 Features
  • 🎪 Regex Patterns: Check regex patterns in JavaScript
  • 🦾 Include Most Useful: Include most useful regex patterns
  • 🌎 No bundler required: Usable via CDN
  • 🔩 Flexible: Configurable event filters and targets

Usage

  1. You can import in your js file es6 (modules,vue,react,...)
import { RX_HREF } from "helping-js/core/regex"; // Check URL

const value = "https://github.com/parsajiravand/helping-js";
if (RX_HREF.test(value)) {
  console.log("Valid URL");
} else {
  console.log("Invalid URL");
}
  1. You can import in your js file es5 (nodejs)
const { RX_HREF } = require("helping-js/core/regex"); // Check URL

const value = "https://github.com/parsajiravand/helping-js";
if (RX_HREF.test(value)) {
  console.log("Valid URL");
} else {
  console.log("Invalid URL");
}
  1. You can usage from CDN (only modules)
import { RX_HREF } from "https://unpkg.com/browse/helping-js/core/regex.js";

const value = "https://github.com/parsajiravand/helping-js";
if (RX_HREF.test(value)) {
  console.log("Valid URL");
} else {
  console.log("Invalid URL");
}

JavaScript Regex Constants

TitleConstRegexSampleDescription
Array NotationRX_ARRAY_NOTATION/\[(\d+)]/garray[0]Get array notation
DigitsRX_DIGITS/^\d+$/123Digits
ExtensionRX_EXTENSION/^\..+/.testFile extension
HashRX_HASH/^#/#testHash
Hash IDRX_HASH_ID/^#[A-Za-z]+[\w\-:.]*$/#testHash ID
HTML TagsRX_HTML_TAGS/(<([^>]+)>)/gi<div>HTML tags
HyphenateRX_HYPHENATE/\B([A-Z])/gteStHyphenate
Lower UpperRX_LOWER_UPPER/([a-z])([A-Z])/gtestTAtLower before upper
NumberRX_NUMBER/^[0-9]*\.?[0-9]+$/123123Just number
Float NumberRX_FLOAT_NUMBER`/^\d+(.\d+)?$^\d+.$/`123.123
PlusRX_PLUS/\+/g+Get plus
Regexp ReplaceRX_REGEXP_REPLACE`/[-/\^$*+?.()[]{}]/g``[-/\^$*+?.()
SpacesRX_SPACES/[\s\uFEFF\xA0]+/g[s\uFEFF\xA0]Get spaces
Space SplitRX_SPACE_SPLIT/\s+/s+First split spaces
Start Space WordRX_START_SPACE_WORD/(^)(\w)/gtestGet start space+charchter space charachters
Trim LeftRX_TRIM_LEFT/^\s+/s+Get spaces from start
Trim RightRX_TRIM_RIGHT/\s+$/s+Get spaces from end
UnderscoreRX_UNDERSCORE/_/g_Get _
Un KebabRX_UN_KEBAB/-\w/g-\wGet -+charchter charchter
DateRX_DATE`/^\d+-\d\d?-\d\d?(?:\sT$)/`
TimeRX_TIME`/^([0-1]?[0-9]2[0-3]):[0-5]?0-9?$/`13:00:00
HrefRX_HREF/^.*(#[^#]+)$/www.google.comHREFs must end with a hash followed by at least one non-hash character
Encoded CommaRX_ENCODED_COMMA/%2C/g%2CEncoded comma
AspectRX_ASPECT/^\d+(\.\d*)?[/:]\d+(\.\d*)?$/1.5Aspect
Aspect SeparatorRX_ASPECT_SEPARATOR/[/:]/:Aspect separator
Strip Locale ModsRX_STRIP_LOCALE_MODS/-u-.+/-u-Strip locale unicode extensions
Strip Locale Mods GlobalRX_STRIP_LOCALE_MODS_GLOBAL/-u-.+/g-u-Strip locale unicode extensions global
Locale ModsRX_LOCALE_MODS/-u-.+/-u-Locale unicode extensions
Locale Mods GlobalRX_LOCALE_MODS_GLOBAL/-u-.+/g-u-Locale unicode extensions global
Locale Mods StartRX_LOCALE_MODS_START/^-u-.+/-u-Locale unicode extensions start
Locale Mods Start GlobalRX_LOCALE_MODS_START_GLOBAL/^-u-.+/g-u-Locale unicode extensions start global
Locale Mods EndRX_LOCALE_MODS_END/-u-.+$/-u-Locale unicode extensions end
Locale Mods End GlobalRX_LOCALE_MODS_END_GLOBAL/-u-.+$/g-u-Locale unicode extensions end global
Locale Mods SplitRX_LOCALE_MODS_SPLIT/-u-/-u-Locale unicode extensions split
Locale Mods Split GlobalRX_LOCALE_MODS_SPLIT_GLOBAL/-u-/g-u-Locale unicode extensions split global
Locale Mods Split StartRX_LOCALE_MODS_SPLIT_START/^-u-/-u-Locale unicode extensions split start
Locale Mods Split Start GlobalRX_LOCALE_MODS_SPLIT_START_GLOBAL/^-u-/g-u-Locale unicode extensions split start global
Locale Mods Split EndRX_LOCALE_MODS_SPLIT_END/-u-$/-u-Locale unicode extensions split end
Locale Mods Split End GlobalRX_LOCALE_MODS_SPLIT_END_GLOBAL/-u-$/g-u-Locale unicode extensions split end global
UppercaseRX_UPPERCASE/^[A-Z]+$/TESTUppercase letters
LowercaseRX_LOWERCASE/^[a-z]+$/testLowercase letters
EmailRX_EMAIL/^[^\s@]+@[^\s@]+\.[^\s@]+$/`
URLRX_URL`/^(https?ftp)://[^\s/$.?#].[^\s]*$/i`https://www.google.com
URL PathRX_URL_PATH`/^(https?ftp)://[^\s/$.?#].[^\s]*$/i`https://www.google.com
URL QueryRX_URL_QUERY`/^(https?ftp)://[^\s/$.?#].[^\s]*$/i`https://www.google.com
URL FragmentRX_URL_FRAGMENT`/^(https?ftp)://[^\s/$.?#].[^\s]*$/i`https://www.google.com
IPRX_IP`/^(https?ftp)://[^\s/$.?#].[^\s]*$/i`https://www.google.com
IPv4RX_IPV4`/^(25[0-5]2[0-4][0-9][01]?[0-9][0-9]?).(25[0-5]
IPv6RX_IPV6`/([0-9a-fA-F]{1,4}😃{7,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}
Hex ColorRX_HEX_COLOR`/^#?([a-fA-F0-9]{6}[a-fA-F0-9]{3})$/`#000
UUIDRX_UUID/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i00000000-0000-0000-0000-000000000000UUID format
UUID V1RX_UUID_V1/^[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i00000000-0000-1000-8000-000000000000UUID v1 format
UUID V2RX_UUID_V2/^[0-9a-f]{8}-[0-9a-f]{4}-2[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i00000000-0000-2000-8000-000000000000UUID v2 format
UUID V3RX_UUID_V3/^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i00000000-0000-3000-8000-000000000000UUID v3 format
UUID V4RX_UUID_V4/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i00000000-0000-4000-8000-000000000000UUID v4 format
UUID V5RX_UUID_V5/^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i00000000-0000-5000-8000-000000000000UUID v5 format
PhoneRX_PHONE/^\+?[0-9]{1,4}?[-. ]?\(?[0-9]{1,3}\)?[-. ]?[0-9]{1,4}[-. ]?[0-9]{1,4}$/+1 (555) 555-5555Phone number
Credit CardRX_CREDIT_CARD`/^(?:4[0-9]{12}(?:[0-9]{3})?5[1-5][0-9]{14}3[47][0-9]{13}
SSNRX_SSN/^\d{3}-\d{2}-\d{4}$/123-45-6789Social security number
SSN PartialRX_SSN_PARTIAL/^\d{3}-\d{2}-\d{1,4}$/123-45-6Partial social security number
SSN Last FourRX_SSN_LAST_FOUR/^\d{4}$/6789Last four of social security number
SSN FullRX_SSN_FULL/^\d{9}$/123456789Full social security number
SSN Full DashRX_SSN_FULL_DASH/^\d{3}-\d{2}-\d{4}$/123-45-6789Full social security number with dashes
SSN Full SpaceRX_SSN_FULL_SPACE/^\d{3} \d{2} \d{4}$/123 45 6789Full social security number with spaces
ISO DateRX_ISO_DATE`/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:.\d+)?(?:Z[+-]\d{2}:\d{2})?$/`1999-01-20T22:51:49.118Z
ISO Date TimeRX_ISO_DATE_TIME/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/1999-01-20T22:51:49ISO date time format
ISO Date Time MSRX_ISO_DATE_TIME_MS/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{1,3}$/1999-01-20T22:51:49.118ISO date time with milliseconds
ISO Date Time TZRX_ISO_DATE_TIME_TZ`/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z[+-]\d{2}:\d{2}$/`1999-01-20T22:51:49Z
ISO Date Time MS TZRX_ISO_DATE_TIME_MS_TZ`/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{1,3}Z[+-]\d{2}:\d{2}$/`1999-01-20T22:51:49.118Z
Time 24HRX_TIME_24H`/^(0?[0-9]1[0-9]2[0-3]):[0-5][0-9]$/`
Time 24H MSRX_TIME_24H_MS`/^(0?[0-9]1[0-9]2[0-3]):[0-5][0-9]:[0-5][0-9].\d{1,3}$/`
Time 24H TZRX_TIME_24H_TZ`/^(0?[0-9]1[0-9]2[0-3]):[0-5][0-9]:[0-5][0-9]Z
HTML CommentRX_HTML_COMMENT//g<!-- test -->HTML comment
HTML TagRX_HTML_TAG/<([a-z]+)([^<]+)(?:>(.)</\1>\s+/>)/gi<div>test</div>
HTML Tag AttributeRX_HTML_TAG_ATTRIBUTE/([a-z-]+)="([^"]*)"/giclass="test"HTML tag attribute
HTML Tag IDRX_HTML_TAG_ID/id="([^"]*)"/giid="test"HTML tag ID
HTML Tag ClassRX_HTML_TAG_CLASS/class="([^"]*)"/giclass="test"HTML tag class
HTML Tag NameRX_HTML_TAG_NAME/<([a-z]+)([^<]+)(?:>(.)</\1>\s+/>)/gi<div>test</div>
HTML Tag ContentRX_HTML_TAG_CONTENT/<([a-z]+)([^<]+)(?:>(.)</\1>\s+/>)/gi<div>test</div>
HTML Tag Self ClosingRX_HTML_TAG_SELF_CLOSING/<([a-z]+)([^<]+)(?:>(.)</\1>\s+/>)/gi<div>test</div>
HTML Tag StartRX_HTML_TAG_START/<([a-z]+)([^<]+)(?:>(.)</\1>\s+/>)/gi<div>test</div>
HTML Tag EndRX_HTML_TAG_END/<([a-z]+)([^<]+)(?:>(.)</\1>\s+/>)/gi<div>test</div>
HTML Tag EmptyRX_HTML_TAG_EMPTY/<([a-z]+)([^<]+)(?:>(.)</\1>\s+/>)/gi<div>test</div>
HTML Tag CommentRX_HTML_TAG_COMMENT/<([a-z]+)([^<]+)(?:>(.)</\1>\s+/>)/gi<div>test</div>
File PathRX_FILE_PATH/^(/?[\w-]+)+/?$//path/to/fileFile path
File Path WindowsRX_FILE_PATH_WINDOWS/^(?:[a-zA-Z]:)?\/*[^\/]+$/C:\path\to\fileFile path for Windows
File Path UnixRX_FILE_PATH_UNIX/^/(?:[^/]+/)*[^/]+$//path/to/fileFile path for Unix
File Path MacRX_FILE_PATH_MAC/^/(?:[^/]+/)*[^/]+$//path/to/fileFile path for Mac
JS IdentifierRX_JS_IDENTIFIER/^[$A-Z_][0-9A-Z_$]*$/itestValid JavaScript identifier
Leading Trailing WhitespaceRX_LEADING_TRAILING_WHITESPACE/^\s+\s+$/gtest
JSON StringRX_JSON_STRING/"(\u[a-fA-F0-9]{4}\[^u][^\"])*"/
JSON NumberRX_JSON_NUMBER/-?\d*.?\d+/123JSON number
JSON BooleanRX_JSON_BOOLEAN/truefalse/true
JSON NullRX_JSON_NULL/null/nullJSON null
JSON ObjectRX_JSON_OBJECT/{.*}/{ "test": "test" }JSON object
JSON ArrayRX_JSON_ARRAY/[.*]/[1, 2, 3]JSON array
JSON ValueRX_JSON_VALUE/(?:"(\u[a-fA-F0-9]{4}\[^u][^\"])*"
JSON PairRX_JSON_PAIR/"(\u[a-fA-F0-9]{4}\[^u][^\"])*"😦?:"(\u[a-fA-F0-9]{4}
JSON Array ElementRX_JSON_ARRAY_ELEMENT/(?:"(\u[a-fA-F0-9]{4}\[^u][^\"])*"
JSON Array Element LastRX_JSON_ARRAY_ELEMENT_LAST/(?:"(\u[a-fA-F0-9]{4}\[^u][^\"])*"
JSON Array Element FirstRX_JSON_ARRAY_ELEMENT_FIRST/(?:"(\u[a-fA-F0-9]{4}\[^u][^\"])*"
JSON Array Element MiddleRX_JSON_ARRAY_ELEMENT_MIDDLE/(?:"(\u[a-fA-F0-9]{4}\[^u][^\"])*"
PasswordRX_PASSWORD/^(?=.[A-Za-z])(?=.\d)[A-Za-z\d]{8,}$/test123Password (minimum 8 characters, at least one letter and one number)
Password UppercaseRX_PASSWORD_UPPERCASE/^(?=.*[A-Z])/TESTPassword uppercase
Password LowercaseRX_PASSWORD_LOWERCASE/^(?=.*[a-z])/testPassword lowercase
Password NumberRX_PASSWORD_NUMBER/^(?=.*\d)/123Password number
Password SpecialRX_PASSWORD_SPECIAL/^(?=.[!@#$%^&])/!@#$%^&*Password special

Sample

RX_ARRAY_NOTATION

This constant represents a regular expression pattern for checking array notation.

Example usage:

import { RX_ARRAY_NOTATION } from "helping-js/core/regex";
const regex = RX_ARRAY_NOTATION;
const str = "array[0]";
console.log(regex.test(str)); // Output: true

RX_DIGITS

This constant represents a regular expression pattern for checking digits.

Example usage:

import { RX_DIGITS } from "helping-js/core/regex";
const regex = RX_DIGITS;
const str = "123";
console.log(regex.test(str)); // Output: true

RX_EXTENSION

This constant represents a regular expression pattern for checking file extension.

Example usage:

import { RX_EXTENSION } from "helping-js/core/regex";
const regex = RX_EXTENSION;
const str = ".js";
console.log(regex.test(str)); // Output: true

RX_HASH

This constant represents a regular expression pattern for checking hash.

Example usage:

import { RX_HASH } from "helping-js/core/regex";
const regex = RX_HASH;
const str = "#test";
console.log(regex.test(str)); // Output: true

RX_HASH_ID

This constant represents a regular expression pattern for checking hash id.

Example usage:

import { RX_HASH_ID } from "helping-js/core/regex";
const regex = RX_HASH_ID;
const str = "#test";
console.log(regex.test(str)); // Output: true

RX_HTML_TAGS

This constant represents a regular expression pattern for checking HTML tags.

Example usage:

import { RX_HTML_TAGS } from "helping-js/core/regex";
const regex = RX_HTML_TAGS;
const str = "<div>";
console.log(regex.test(str)); // Output: true

RX_HYPHENATE

This constant represents a regular expression pattern for checking hyphenate.

Example usage:

import { RX_HYPHENATE } from "helping-js/core/regex";
const regex = RX_HYPHENATE;
const str = "teSt";
console.log(regex.test(str)); // Output: true

RX_LOWER_UPPER

This constant represents a regular expression pattern for checking lower upper.

Example usage:

import { RX_LOWER_UPPER } from "helping-js/core/regex";
const regex = RX_LOWER_UPPER;
const str = "testTAt";
console.log(regex.test(str)); // Output: true

RX_NUMBER

This constant represents a regular expression pattern for checking number.

Example usage:

import { RX_NUMBER } from "helping-js/core/regex";
const regex = RX_NUMBER;
const str = "123123";
console.log(regex.test(str)); // Output: true

RX_FLOAT_NUMBER

This constant represents a regular expression pattern for checking float number.

Example usage:

import { RX_FLOAT_NUMBER } from "helping-js/core/regex";
const regex = RX_FLOAT_NUMBER;
console.log(regex.test("123.123")); // Output: true
console.log(regex.test("60")); // Output:  true
console.log(regex.test("60.5")); // Output:  true
console.log(regex.test("60.")); // Output:  true
console.log(regex.test(".5")); // Output:  false
console.log(regex.test("abc")); // Output:  false
console.log(regex.test("123.456")); // Output:  true
console.log(regex.test("0.456")); // Output:  true

RX_PLUS

This constant represents a regular expression pattern for checking plus.

Example usage:

import { RX_PLUS } from "helping-js/core/regex";
const regex = RX_PLUS;
const str = "+";
console.log(regex.test(str)); // Output: true

RX_REGEXP_REPLACE

This constant represents a regular expression pattern for checking regexp replace.

Example usage:

import { RX_REGEXP_REPLACE } from "helping-js/core/regex";
const regex = RX_REGEXP_REPLACE;
const str = "[-/\\^$*+?.()|[]{}]";
console.log(regex.test(str)); // Output: true

RX_SPACES

This constant represents a regular expression pattern for checking spaces.

Example usage:

import { RX_SPACES } from "helping-js/core/regex";
const regex = RX_SPACES;
const str = "[s\uFEFF\xA0]";
console.log(regex.test(str)); // Output: true

RX_SPACE_SPLIT

This constant represents a regular expression pattern for checking space split.

Example usage:

import { RX_SPACE_SPLIT } from "helping-js/core/regex";
const regex = RX_SPACE_SPLIT;
const str = "s+";
console.log(regex.test(str)); // Output: true

RX_START_SPACE_WORD

This constant represents a regular expression pattern for checking start space word.

Example usage:

import { RX_START_SPACE_WORD } from "helping-js/core/regex";
const regex = RX_START_SPACE_WORD;
const str = "test";
console.log(regex.test(str)); // Output: true

RX_TRIM_LEFT

This constant represents a regular expression pattern for checking trim left.

Example usage:

import { RX_TRIM_LEFT } from "helping-js/core/regex";
const regex = RX_TRIM_LEFT;
const str = "s+";
console.log(regex.test(str)); // Output: true

RX_TRIM_RIGHT

This constant represents a regular expression pattern for checking trim right.

Example usage:

import { RX_TRIM_RIGHT } from "helping-js/core/regex";
const regex = RX_TRIM_RIGHT;
const str = "s+";
console.log(regex.test(str)); // Output: true

RX_UNDERSCORE

This constant represents a regular expression pattern for checking underscore.

Example usage:

import { RX_UNDERSCORE } from "helping-js/core/regex";
const regex = RX_UNDERSCORE;
const str = "_";
console.log(regex.test(str)); // Output: true

RX_UN_KEBAB

This constant represents a regular expression pattern for checking un kebab.

Example usage:

import { RX_UN_KEBAB } from "helping-js/core/regex";
const regex = RX_UN_KEBAB;
const str = "-+charchter charchter";
console.log(regex.test(str)); // Output: true

RX_DATE

This constant represents a regular expression pattern for checking date.

Example usage:

import { RX_DATE } from "helping-js/core/regex";
const regex = RX_DATE;
const str = "1999-01-20T22:51:49.118Z";
console.log(regex.test(str)); // Output: true

RX_TIME

This constant represents a regular expression pattern for checking time.

Example usage:

import { RX_TIME } from "helping-js/core/regex";
const regex = RX_TIME;
const str = "13:00:00";
console.log(regex.test(str)); // Output: true

RX_HREF

This constant represents a regular expression pattern for checking href.

Example usage:

import { RX_HREF } from "helping-js/core/regex";
const regex = RX_HREF;
const str = "www.google.com";
console.log(regex.test(str)); // Output: true

RX_ENCODED_COMMA

This constant represents a regular expression pattern for checking encoded comma.

Example usage:

import { RX_ENCODED_COMMA } from "helping-js/core/regex";
const regex = RX_ENCODED_COMMA;
const str = "%2C";
console.log(regex.test(str)); // Output: true

RX_ENCODE_REVERSE

This constant represents a regular expression pattern for checking encode reverse.

Example usage:

import { RX_ENCODE_REVERSE } from "helping-js/core/regex";
const regex = RX_ENCODE_REVERSE;
const str = "!";
console.log(regex.test(str)); // Output: true

RX_QUERY_START

This constant represents a regular expression pattern for checking query start.

Example usage:

import { RX_QUERY_START } from "helping-js/core/regex";
const regex = RX_QUERY_START;
const str = "?";
console.log(regex.test(str)); // Output: true

RX_ASPECT

This constant represents a regular expression pattern for checking aspect.

Example usage:

import { RX_ASPECT } from "helping-js/core/regex";
const regex = RX_ASPECT;
const str = "1:1";
console.log(regex.test(str)); // Output: true

RX_ASPECT_SEPARATOR

This constant represents a regular expression pattern for checking aspect separator.

Example usage:

import { RX_ASPECT_SEPARATOR } from "helping-js/core/regex";
const regex = RX_ASPECT_SEPARATOR;
const str = ":";
console.log(regex.test(str)); // Output: true

RX_STRIP_LOCALE_MODS

This constant represents a regular expression pattern for checking strip locale mods.

Example usage:

import { RX_STRIP_LOCALE_MODS } from "helping-js/core/regex";
const regex = RX_STRIP_LOCALE_MODS;
const str = "-u-";
console.log(regex.test(str)); // Output: true
Last Updated:
Contributors: parsajiravand, parsa jiravand