JavaScript RegExp Assertions
Position-based matching in regular expressions
📍 What are Assertions?
Assertions in regular expressions match positions, not characters. They help you specify where in the text your pattern should match - at the beginning, end, or around specific positions.
// Assertions match positions, not characters
let text = "JavaScript is awesome";
let startPattern = /^Java/; // ^ means "start of string"
console.log(startPattern.test(text)); // true
Types of Assertions
^ - Start of String
Matches at the beginning of text
$ - End of String
Matches at the end of text
\\b - Word Boundary
Matches edges between words
Lookahead/Lookbehind
Check context without consuming
🔹 Start and End Anchors (^ and $)
Use ^ to match at the beginning and $ to match at the end of strings:
How anchors work:
- ^ - Ensures pattern starts at beginning
- $ - Ensures pattern ends at the end
- ^...$ - Matches entire string exactly
- Multiline mode: ^ and $ match line breaks too
let text = "JavaScript is great";
// Match at start of string
let startPattern = /^Java/;
console.log(startPattern.test(text)); // true
console.log(/^Script/.test(text)); // false
// Match at end of string
let endPattern = /great$/;
console.log(endPattern.test(text)); // true
console.log(/Java$/.test(text)); // false
// Match entire string
let fullPattern = /^JavaScript is great$/;
console.log(fullPattern.test(text)); // true
Output:
Start with "Java": true
Start with "Script": false
End with "great": true
Exact match: true
🔹 Word Boundaries (\\b and \\B)
\\b matches word boundaries, \\B matches non-word boundaries:
🔸 Understanding Word Boundaries
- Word characters: Letters, digits, underscore (a-z, A-Z, 0-9, _)
- Non-word characters: Spaces, punctuation, symbols
- \\b: Boundary between word and non-word character
- \\B: Position that is NOT a word boundary
let text = "The cat in the hat";
// Match whole word "cat"
let wholeWord = /\bcat\b/;
console.log(wholeWord.test(text)); // true
console.log(wholeWord.test("category")); // false (cat is part of word)
// Find all whole words starting with 'th'
let thWords = text.match(/\bth\w*/gi);
console.log(thWords); // ["The", "the"]
// Match "at" only when NOT at word boundary
let notBoundary = /\Bat\B/;
console.log(notBoundary.test("cat")); // true (at is inside cat)
console.log(notBoundary.test("at")); // false (at is whole word)
Output:
Whole word "cat": true
Words starting with "th": ["The", "the"]
"at" inside word: true
"at" as whole word: false
🔹 Positive Lookahead (?=...)
Lookahead checks what comes after without including it in the match:
🔸 How Positive Lookahead Works
- Syntax: (?=pattern)
- Purpose: Match only if followed by specific pattern
- Zero-width: Doesn't consume characters
- Use case: Conditional matching based on context
let text = "password123";
// Match "password" only if followed by digits
let pattern = /password(?=\d+)/;
console.log(pattern.test(text)); // true
console.log(pattern.test("password")); // false
// Extract the match (doesn't include the digits)
let match = text.match(/password(?=\d+)/);
console.log(match[0]); // "password"
// Find words followed by numbers
let text2 = "item1 item2 item";
let itemsWithNumbers = text2.match(/item(?=\d)/g);
console.log(itemsWithNumbers); // ["item", "item"]
Output:
Password with digits: true
Password without digits: false
Match: "password"
Items with numbers: ["item", "item"]
🔹 Negative Lookahead (?!...)
Negative lookahead checks that something does NOT come after:
🔸 Negative Lookahead Applications
- Syntax: (?!pattern)
- Purpose: Match only if NOT followed by pattern
- Exclusion: Filter out unwanted matches
- Validation: Ensure certain conditions aren't met
let passwords = ["password123", "password", "secret456"];
// Match "password" NOT followed by digits
let pattern = /password(?!\d)/;
passwords.forEach(pwd => {
console.log(`${pwd}: ${pattern.test(pwd)}`);
});
// password123: false (followed by digits)
// password: true (not followed by digits)
// secret456: false (doesn't contain "password")
// Find files that are NOT .txt
let files = ["doc.txt", "image.jpg", "data.csv"];
let nonTxtFiles = files.filter(file => /\w+(?!\.txt$)/.test(file));
console.log(nonTxtFiles); // ["image.jpg", "data.csv"]
Output:
password123: false
password: true
secret456: false
Non-txt files: ["image.jpg", "data.csv"]