JavaScript RegExp Assertions

Back to JS page

Table of Contents

RegExp Assertions

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

RegExp ^ Metacharacter

^ 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>

Example 1

Result View Example

RegExp $ Metacharacter

$ 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>

Example 2

Result View Example

The \b Metacharacter

\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>

Example 3

Result View Example

RegExp Lookahead x(?=y)

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>

Example 4

Result View Example

Negative Lookahead x(?!y)

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>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference