🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #87

JS RegExp Assertions

RegExp Assertions

Assertions matches Boundaries and Lookarounds:

String Boundaries and Word Boundaries.

Lookarounds: Lookaheads and Lookbehinds.

// Match beginning of string

const pattern = /^W3Schools/;

// Match end of string

const pattern = /W3Schools$/;

JavaScript Regex Assertions

Revised July 2025

Col 1 Col 2 Col 3
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

const pattern = /^W3Schools/;

let text = "W3Schools Tutorial";

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

RegExp $ Metacharacter

The $ metacharacter matches the end of a string.

const pattern = /W3Schools$/;

let text = "Hello W3Schools";

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

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

let text = "W3Schools Tutorials";

let pattern = /W3Schools(?= Tutorials)/;

let result = pattern.test(text);

Negative Lookahead x(?!y)

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

Example

let text = "W3Schools Tutorials";

let pattern = /W3Schools(?! Tutorials)/;

let result = pattern.test(text);

Explanation

W3Schools matches the literal word.

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

If text = "W3Schools Tutorials", the test returns false.

If text = "W3Schools Website", the test returns true.


RegExp Lookbehind (?<=y)x

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

Example

let text = "Hello W3Schools";

let pattern = /(?<=Hello )W3Schools/;

let result = pattern.test(text);

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);

Note: 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

Col 1 Col 2
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

Col 1 Col 2
Method Description
regex.exec() Returns an Iterator of results
regex.test() Returns true or false

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS RegExp Assertions – FAQs

Quick answers about learning JS RegExp Assertions in JavaScript.

This free note from CodingNow 2.0 explains JS RegExp Assertions in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including JS RegExp Assertions, is 100% free with no signup required.
With focused practice, most students grasp JS RegExp Assertions in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now