Regular Expression Assertions
RegExp Assertions
Assertions consist of Boundaries and Lookarounds:
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 W3Schools:
const pattern = /^W3Schools/;
let text = "W3Schools Tutorial";
let result = pattern.test(text); // true
const pattern = /^W3Schools/;
let text = "Hello W3Schools";
let result = pattern.test(text); // false
RegExp $ Metacharacter
The $ metacharacter matches the end of a string.
Test if a string ends with W3Schools:
const pattern = /W3Schools$/;
let text = "Hello W3Schools";
let result = pattern.test(text); // true
Try it Yourself »
const pattern = /W3Schools$/;
let text = "W3Schools 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 "W3Schools" is followed by " Tutorials".
let text = "W3Schools Tutorials";
let pattern = /W3Schools(?= 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 = "Hello W3Schools";
let pattern = /W3Schools(?!Hello )/;
let result = pattern.test(text);
Try it Yourself »
RegExp Lookbehind (?<=y)x
(?<=y)x matches "x" if "x" is preceded by "y".
Example
Match "W3Scools" if "W3Schools" is preceded by "Hello ".
let text = "Hello W3Schools";
let pattern = /(?<=Hello )W3Schools/;
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 W3Schools";
let pattern = /(?<!Hello ) W3Schools/;
let result = pattern.test(text);
Try it Yourself »
Regular Expression Groups
Char | Description |
---|---|
(x) | Matches x and remembers the match |
(?<n>x) | Matches x and labels it n |
(?flag:x) | Enables flag(s) only to the group |
(?flag-flag:x) | Disables flag(s) only to the group |
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 |