Regular Expression Assertions
RegExp Assertions
Assertions matches Boundaries and Lookarounds:
String Boundaries and Word Boundaries.
Lookarounds: Lookaheads and Lookbehinds.
// Match beginning of string
const pattern = /^Webmastertoolbag/;
// Match end of string
const pattern = /Webmastertoolbag$/;
JavaScript Regex Assertions
Revised July 2025
| Syntax | Name | Description |
|---|---|---|
| ^ | String boundary | Matches the beginning of a string |
| $ | String boundary | Matches the end of a string |
| \b | Word boundary | Matches the beginning or end of a word |
| \B | Word boundary | Matches NOT the beginning or end of a word |
| (?=...) | Lookahead | Matches the subsequent string |
| (?!...) | Lookahead | Matches NOT the subsequent string |
| (?<=...) | Lookbehind | Matches the previous string |
| (?<!...) | Lookbehind | Matches NOT the previous string |
RegExp ^ Metacharacter
The ^ metacharacter matches the beginning of a string.
Examples
Test if a string starts with Webmastertoolbag:
const pattern = /^Webmastertoolbag/;
let text = "Webmastertoolbag Tutorial";
let result = pattern.test(text); // true
const pattern = /^Webmastertoolbag/;
let text = "Hello Webmastertoolbag";
let result = pattern.test(text); // false
RegExp $ Metacharacter
The $ metacharacter matches the end of a string.
Test if a string ends with Webmastertoolbag:
const pattern = /Webmastertoolbag$/;
let text = "Hello Webmastertoolbag";
let result = pattern.test(text); // true
Try it Yourself »
const pattern = /Webmastertoolbag$/;
let text = "Webmastertoolbag tutorial";
let result = pattern.test(text); // false
Try it Yourself »
The \b Metacharacter
The \b metacharacter matches the beginning of a word or the end of a word.
Examples
Search for the characters "LO" at the beginning of a word:
let text = "HELLO, LOOK AT YOU!";
let result = text.search(/\bLO/);
Search for the characters "LO" at the end of a word:
let text = "HELLO, LOOK AT YOU!";
let result = text.search(/LO\b/);
RegExp Lookahead x(?=y)
x(?=y) matches "x" if "x" is followed by "y".
Example
Match "W3schools" if "Webmastertoolbag" is followed by " Tutorials".
let text = "Webmastertoolbag Tutorials";
let pattern = /Webmastertoolbag(?= Tutorials)/;
let result = pattern.test(text);
Try it Yourself »
Negative Lookahead x(?!y)
x(?!y) matches "x" if "x" is NOT followed by "y".
Example
let text = "Webmastertoolbag Tutorials";
let pattern = /Webmastertoolbag(?! Tutorials)/;
let result = pattern.test(text);
Try it Yourself »
Explanation
Webmastertoolbag matches the literal word.
(?! Tutorials) asserts that what follows is not " Tutorials".
If text = "Webmastertoolbag Tutorials", the test returns false.
If text = "Webmastertoolbag Website", the test returns true.
RegExp Lookbehind (?<=y)x
(?<=y)x matches "x" if "x" is preceded by "y".
Example
Match "W3Scools" if "Webmastertoolbag" is preceded by "Hello ".
let text = "Hello Webmastertoolbag";
let pattern = /(?<=Hello )Webmastertoolbag/;
let result = pattern.test(text);
Try it Yourself »
Negative Lookbehind (?<!y)x
(?<!y)x matches "x" only if "x" is NOT preceded by "y".
Example
let text = "Hello Webmastertoolbag";
let pattern = /(?<!Hello )Webmastertoolbag/;
let result = pattern.test(text);
Try it Yourself »
Regular Expression Methods
Regular Expression Search and Replace can be done with different methods.
These are the most common:
String Methods
| Method | Description |
|---|---|
| match(regex) | Returns an Array of results |
| matchAll(regex) | Returns an Iterator of results |
| replace(regex) | Returns a new String |
| replaceAll(regex) | Returns a new String |
| search(regex) | Returns the index of the first match |
| split(regex) | Returns an Array of results |
RegExp Methods
| Method | Description |
|---|---|
| regex.exec() | Returns an Iterator of results |
| regex.test() | Returns true or false |
