⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

JavaScript Regular Expressions

A regular expression is a sequence of characters that form a search pattern and can be simple or complex. It is used for searching, replacing, and validating text. In JavaScript, regex is commonly used with string methods: search() (returns the starting position of the first match), replace() (returns a new string with replaced matches), and match() (returns an array of matches). On the RegExp object, test() (checks whether a string matches the pattern) and exec() (returns match details) are available.

EXAMPLE
RESULT

Creating and Modifiers of Regular Expressions

A regular expression (regex) is a pattern for searching in text. In JavaScript, it is created with slashes /pattern/modifiers or with the RegExp() constructor. Modifiers (flags) determine the search mode: i (ignores case), g (global search for all matches), and m (multiline search).

Methods for Working with Regular Expressions

Two types of methods are available for working with regex: those on the RegExp object (test(), exec()) and those on the String object (search(), match(), replace()). Each has its advantage: test() is the fastest for validation, exec() returns details, match() returns all matches, and replace() enables substitution.

Overview of Symbols, Quantifiers, and Common Patterns

The table shows all basic symbols (e.g., \d, \w), quantifiers (e.g., *, +, {n,m}), and examples of common patterns (email, phone, date). For each element, the description, syntax, and usage example are provided.

Name / Pattern Description Syntax / Example Note
Creating Regex A regular expression is created with slashes or a constructor. /pattern/modifiers
new RegExp("pattern", "modifiers")
Literals are faster. The constructor is useful when the pattern is dynamic (from a string).
i (ignoreCase) Does not distinguish between uppercase and lowercase letters. /abc/i Matches "abc", "ABC", "Abc", "aBc", etc.
g (global) Searches for all matches (not just the first). /a/g Without this flag, it stops at the first match.
m (multiline) Enables searching at the beginning and end of each line. /^start/m Anchors ^ and $ work at the start/end of each line, not just the entire string.
s (dotAll) The dot (.) also matches spaces and newlines. /a.b/s Useful for multiline searching (supported in newer browsers).
test() Returns true if the string matches the pattern, otherwise false. pattern.test(string) The fastest way to validate (e.g., form validation).
exec() Returns an array with details of the first match or null. pattern.exec(string) Also returns the match index and groups. Useful in loops to find all matches.
search() Returns the index of the first match or -1. string.search(pattern) Ignores the g flag (always returns only the first match).
match() Returns an array of all matches or null. string.match(pattern) With the g flag, returns only matched strings. Without g, returns details (like exec).
matchAll() Returns an iterator of all matches with details. ...string.matchAll(pattern) Must have the g flag. Useful for iterating through all matches.
replace() Returns a new string with replaced matches. string.replace(pattern, "new_string") Without g, replaces only the first. With g, replaces all.
replaceAll() Returns a new string with all occurrences replaced. string.replaceAll("old", "new") Same as replace with g, but without needing regex for simple replacements.
. Matches any character except a newline. /a.b/ Matches "aab", "acb", "a1b", but not "a\nb" (unless with the s flag).
^ Matches the beginning of a string (or line with m). /^abc/ Matches "abc...", but not "...abc".
$ Matches the end of a string (or line with m). /abc$/ Matches "...abc", but not "abc...".
[] Matches any character within the brackets. [abc] Matches "a", "b", or "c".
[^] Matches any character NOT within the brackets. [^abc] Matches everything except "a", "b", "c".
| Or (alternation). a|b Matches "a" or "b".
() Group (capturing group). (abc) Saves the captured group for later use (e.g., in replace or exec).
\d Matches a digit (0-9). \d+ Equivalent to [0-9].
\D Matches everything except digits. \D+ Equivalent to [^0-9].
\w Matches a letter, digit, or underscore (word character). \w+ Equivalent to [a-zA-Z0-9_].
\W Matches everything except letters, digits, or underscores. \W+ Equivalent to [^a-zA-Z0-9_].
\s Matches a whitespace character (space, tab, newline). \s+ Useful for cleaning up strings.
\S Matches everything except whitespace characters. \S+ Useful for finding words.
* 0 or more times. ab*c Matches "ac", "abc", "abbc", "abbbc", etc.
+ 1 or more times. ab+c Matches "abc", "abbc", but not "ac".
? 0 or 1 time (optional). colou?r Matches "color" and "colour".
{n} Exactly n times. \d{3} Matches exactly 3 digits (e.g., "123").
{n,} At least n times. \d{3,} Matches 3 or more digits.
{n,m} Between n and m times. \d{2,4} Matches 2, 3, or 4 digits.
Email Basic email validation. /^[^\s@]+@[^\s@]+\.[^\s@]+$/ Checks the basic structure (local@domain.tld).
Phone Number Format 040 123 456 or +386 40 123 456. /^\+?[0-9\s\-]{7,15}$/ Checks whether the number is between 7 and 15 characters (digits, spaces, hyphens, plus).
Date (YYYY-MM-DD) Date format validation. /^\d{4}-\d{2}-\d{2}$/ Only checks the format, not the validity of the date (e.g., 2024-13-45 would be "valid" by this regex).
Color (HEX) HEX color validation (#RGB or #RRGGBB). /^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$/ Matches "#FFF" or "#FFFFFF".