Table of Contents
Assertions are conditions that must be true at a position in the string:
| Assertion | Description |
|---|---|
^ |
Matches the beginning of the string |
$ |
Matches the end of the string |
\b |
Matches at a word boundary |
x(?=y) |
Lookahead: x only if followed by y |
x(?!y) |
Negative lookahead: x only if NOT followed by y |
(?<=y)x |
Lookbehind: x only if preceded by y |
(?<!y)x |
Negative lookbehind: x only if NOT preceded by y |
^ asserts the position at the start of the string:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Assertions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp ^ Metacharacter</h4>
<p id="demo"></p>
<script>
const text = "Hello World, Hello Again";
const matches = text.match(/^Hello/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/^Hello/g): " + matches + "<br>" +
"^ matches 'Hello' only at the start";
</script>
</body>
</html>

Result View Example
$ asserts the position at the end of the string:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Assertions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp $ Metacharacter</h4>
<p id="demo"></p>
<script>
const text = "Hello World, Hello";
const matches = text.match(/Hello$/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/Hello$/g): " + matches + "<br>" +
"$ matches 'Hello' only at the end";
</script>
</body>
</html>

Result View Example
\b asserts a word boundary - where a word starts or ends:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Assertions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The \b Metacharacter</h4>
<p id="demo"></p>
<script>
const text = "car cart scar";
const matches = text.match(/\bcar\b/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/\\bcar\\b/g): " + matches + "<br>" +
"Finds 'car' as a whole word only";
</script>
</body>
</html>

Result View Example
Lookahead matches x only if it is followed by y:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Assertions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RegExp Lookahead x(?=y)</h4>
<p id="demo"></p>
<script>
const text = "apple banana applepie";
const matches = text.match(/apple(?=pie)/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/apple(?=pie)/g): " + matches + "<br>" +
"Matches 'apple' only when followed by 'pie'";
</script>
</body>
</html>

Result View Example
Negative lookahead matches x only if it is NOT followed by y:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegExp Assertions</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Negative Lookahead x(?!y)</h4>
<p id="demo"></p>
<script>
const text = "apple banana applepie";
const matches = text.match(/apple(?!pie)/g);
document.getElementById("demo").innerHTML =
"Text: " + text + "<br><br>" +
"match(/apple(?!pie)/g): " + matches + "<br>" +
"Matches 'apple' only when NOT followed by 'pie'";
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.