Open Playground
JS TutorialLesson #94

Regular Expression Assertions

Assertions matches Boundaries and Lookarounds:

RegExp Assertions

Assertions matches Boundaries and Lookarounds:

String Boundaries and Word Boundaries.

Lookarounds: Lookaheads and Lookbehinds.

// Match beginning of string const pattern = /^EA Academy/;

// Match end of string const pattern = /EA Academy$/;

JavaScript Regex Assertions

Revised July 2025

SyntaxNameDescription
^String boundaryMatches the beginning of a string
$String boundaryMatches the end of a string
\bWord boundaryMatches the beginning or end of a word
\BWord boundaryMatches NOT the beginning or end of a word
(?=...)LookaheadMatches the subsequent string
(?!...)LookaheadMatches NOT the subsequent string

| (?


RegExp ^ Metacharacter

The ^ metacharacter matches the beginning of a string.

Examples

Example Code

javascript
Try it Yourself
const pattern = /^EA Academy/;

let text = "EA Academy Tutorial";

let result = pattern.test(text); // true

RegExp $ Metacharacter

The $ metacharacter matches the end of a string.

Example

Example Code

javascript
Try it Yourself
const pattern = /EA Academy$/;

let text = "Hello EA Academy";

let result = pattern.test(text); // true

The \b Metacharacter

The \b metacharacter matches the beginning of a word or the end of a word.

Examples

Example Code

javascript
Try it Yourself
let text = "HELLO, LOOK AT YOU!";

let result = text.search(/\bLO/);

RegExp Lookahead x(?=y)

x(?=y) matches "x" if "x" is followed by "y".

Example

Example Code

javascript
Try it Yourself
let text = "EA Academy Tutorials";

let pattern = /EA Academy(?= Tutorials)/;

let result = pattern.test(text);

Negative Lookahead x(?!y)

x(?!y) matches "x" if "x" is NOT followed by "y".

Example

Example Code

javascript
Try it Yourself
let text = "EA Academy Tutorials";

let pattern = /EA Academy(?! Tutorials)/;

let result = pattern.test(text);

Explanation

EA Academy matches the literal word.

(?! Tutorials) asserts that what follows is not " Tutorials".

If text = "EA Academy Tutorials", the test returns false.

If text = "EA Academy Website", the test returns true.


RegExp Lookbehind (? **See Also:** > JavaScript RegExp Tutorial > JavaScript RegExp Flags > JavaScript RegExp Character Classes > JavaScript RegExp Meta Characters > JavaScript RegExp Groups > JavaScript RegExp Quantifiers > JavaScript RegExp Patterns > JavaScript RegExp Objects > JavaScript RegExp Methods


Regular Expression Methods

Regular Expression Search and Replace can be done with different methods.

These are the most common:

String Methods

MethodDescription
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

MethodDescription
regex.exec()Returns an Iterator of results
regex.test()Returns true or false

Interactive Code Examples

Examples

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const pattern = /^EA Academy/;

let text = "EA Academy Tutorial";



let result = pattern.test(text); // true

Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const pattern = /EA Academy$/;

let text = "Hello EA Academy";



let result = pattern.test(text); // true

Examples

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let text = "HELLO, LOOK AT YOU!";


 
let result = text.search(/\bLO/);

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let text = "EA Academy Tutorials";

let pattern = /EA Academy(?= Tutorials)/;



let result = pattern.test(text);

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let text = "EA Academy Tutorials";



let pattern = /EA Academy(?! Tutorials)/;

let result = pattern.test(text);

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let text = "Hello EA Academy";

let pattern = /(?<=Hello )EA Academy/;



let result = pattern.test(text);