JavaScript Regular Expressions (RegEx)
1. Introduction to Regular Expressions
A regular expression (RegEx) is a sequence of characters that forms a search
pattern.
It is mainly used for pattern matching and string manipulation (like searching,
replacing, and validating input).
In JavaScript, RegEx can be created in two ways:
// Using literal notation
let pattern1 = /hello/;
// Using RegExp constructor
let pattern2 = new RegExp("hello");
RegEx patterns are placed between slashes / when using literals.
They are case-sensitive by default.
2. Why Use Regular Expressions?
Validate user input (e.g., emails, phone numbers, passwords).
Search and replace text.
Extract specific parts of a string.
Perform complex text matching.
3. RegEx Methods in JavaScript
3.1 test()
Checks if a pattern exists in a string. Returns true/false.
let regex = /hello/;
[Link]([Link]("hello world")); // true
[Link]([Link]("Hi there")); // false
3.2 exec()
Returns the first match as an array (with details) or null if no match.
let regex = /world/;
[Link]([Link]("hello world"));
// ["world", index: 6, input: "hello world", groups: undefined]
3.3 String Methods with RegEx
match() → Returns array of matches.
search() → Returns index of first match.
replace() → Replaces matches with new text.
split() → Splits string into array using regex.
Example:
let text = "apple, banana, orange";
[Link]([Link](/\w+/g));
// ["apple", "banana", "orange"]
[Link]([Link](/banana/));
// 7
[Link]([Link](/banana/, "grape"));
// apple, grape, orange
[Link]([Link](/,\s*/));
// ["apple", "banana", "orange"]
4. Regular Expression Syntax
4.1 Basic Patterns
/abc/ → matches "abc".
/^abc/ → must start with "abc".
/abc$/ → must end with "abc".
/a.c/ → "a", any character, then "c" (e.g., "abc", "axc").
4.2 Character Classes
[abc] → matches a, b, or c.
[^abc] → matches anything except a, b, or c.
[0-9] → matches any digit.
[a-z] → matches any lowercase letter.
[A-Z] → matches uppercase letters.
Example:
let regex = /[0-9]/;
[Link]([Link]("car123")); // true
4.3 Predefined Character Classes
\d → digit (0–9).
\D → non-digit.
\w → word character (letters, digits, underscore).
\W → non-word character.
\s → whitespace.
\S → non-whitespace.
. → any character except newline.
4.4 Quantifiers
a+ → one or more "a".
a* → zero or more "a".
a? → zero or one "a".
a{3} → exactly 3 "a".
a{2,4} → between 2 and 4 "a".
a{2,} → at least 2 "a".
Example:
[Link](/a{3}/.test("caaaandy")); // true
[Link](/a+/.test("candy")); // true
4.5 Alternation and Grouping
(abc) → groups expressions.
a|b → matches either "a" or "b".
let regex = /(cat|dog)/;
[Link]([Link]("I have a dog")); // true
4.6 Anchors
^ → start of string.
$ → end of string.
\b → word boundary.
[Link](/^Hello/.test("Hello world")); // true
[Link](/world$/.test("Hello world")); // true
5. Flags in Regular Expressions
i → case-insensitive.
g → global search (all matches).
m → multiline mode.
let regex = /hello/gi;
[Link]("Hello hello world".match(regex));
// ["Hello", "hello"]
6. Practical Examples
6.1 Validate Email
let emailPattern = /^[\w.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
[Link]([Link]("user@[Link]")); // true
[Link]([Link]("invalid-email")); // false
6.2 Validate Phone Number (10 digits)
let phonePattern = /^\d{10}$/;
[Link]([Link]("9876543210")); // true
[Link]([Link]("98765abc")); // false
6.3 Validate Password (at least 8 chars, one number, one uppercase)
let passwordPattern = /^(?=.*[A-Z])(?=.*\d).{8,}$/;
[Link]([Link]("Password1")); // true
[Link]([Link]("password")); // false
7. Summary
Regular expressions are powerful tools for pattern matching and
validation.
JavaScript provides built-in methods like test(), exec(), match(), search(),
replace(), split().
Use anchors, quantifiers, and character classes to build complex rules.
Widely used for form validation (emails, passwords, numbers, etc.).
✅ This lesson can span 2 lectures:
Lecture 1: Basics of RegEx, syntax, quantifiers, anchors, string methods.
Lecture 2: Flags, grouping, validation examples, mini-projects (form
validation).