JavaScript RegExp $ End Anchor

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $?

$ is a zero-width end anchor. It matches a position—not a character—at the end of the input. With the m flag, it also matches at the end of each line. Use it to require a suffix.

Remember
/pattern$/          // must finish at end of string
/pattern$/m         // end of each line too

/world$/.test("hello world")  → true
/world$/.test("world peace")  → false

$ consumes nothing (zero-width)
pair with ^ for full-string match: /^…$/
UsePrefer
Ends with a word/world$/
End of each line/end$/m or /end$/gm
File extension/\.png$/i

How to Use $

Put $ at the end of the pattern (or before a trailing flag).

  1. Write the text or group that must appear last, then add $ — e.g. /ing$/.
  2. Call test, match, or replace as usual.
  3. Add m when you need the end of every line in multi-line text.
  4. Do not confuse pattern $ with $1 in a replace replacement string.
PieceRequired?Role
$YesAssert end of string (or line with m)
Suffix patternUsualWhat must appear just before the end
m flagOptionalTreat each line end as a match position

Pseudocode

Pseudocode
re = /suffix$/

find "suffix" in text
if found and position after it is end-of-input
   (or end-of-line when m is on):
    match ✓
else:
    fail

// $ itself matches no characters

Cheat Sheet

Keep these end-anchor facts handy.

TopicDetail
Syntax/pattern$/
DefaultEnd of the whole string
With mAlso before each newline
Zero-widthDoes not consume characters
vs ^^ = start; $ = end
vs \b\b = word edge; $ = string/line end
In replace$1 = group; pattern $ is different
StandardES3+ (universal)

Live Preview

Test end-anchor patterns against sample strings.

Browser demo of the $ end anchor — same idea as the examples below.

Result
/world$/.test("hello world")
→ true

/* "world" sits at the end of the string */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Require a Suffix with $

Match only when the word sits at the end of the string.

JavaScript
console.log(/world$/.test("hello world"));  // true
console.log(/world$/.test("say world"));    // true
console.log(/world$/.test("world peace"));  // false
Try It Yourself

How It Works

  1. Find the text. The engine looks for "world".
  2. Check the end. After that match, $ requires no more characters.
  3. Success cases. "hello world" and "say world" end with world.
  4. Failure case. In "world peace", characters continue after world → false.

Example 2 — End of Each Line with $ and m

Match a suffix on every line, not only the last line of the string.

JavaScript
const text = "line one end\nline two end\nno match";

console.log(/end$/.test(text));     // false
console.log(/end$/m.test(text));    // true
console.log(text.match(/end$/gm));  // ["end", "end"]
Try It Yourself

How It Works

  1. Without m. $ looks at the whole string end — "no match" → false.
  2. With m. $ also matches before each newline.
  3. Lines 1 and 2 win. Both end with "end".
  4. gm collects them. You get two matches from the first two lines.

Example 3 — Validate a File Extension

Require the name to end with .jpg, .jpeg, or .png.

JavaScript
const isImage = /\.(jpe?g|png)$/i;

console.log(isImage.test("photo.jpg"));      // true
console.log(isImage.test("photo.JPEG"));     // true
console.log(isImage.test("photo.jpg.bak"));  // false
Try It Yourself

How It Works

  1. Escape the dot. \. means a literal period, not “any character.”
  2. Allow variants. jpe?g matches jpg or jpeg; |png adds PNG.
  3. Anchor the end. $ rejects names like photo.jpg.bak.
  4. Ignore case. The i flag accepts .JPEG as well.

Edge Cases & Pitfalls

Check these before relying on $.

replace

Pattern $ ≠ replacement $1

In a pattern, $ is an end anchor. In a replacement string, $1 means capture group 1 and $$ is a literal dollar.

newline

Trailing \n can still match

In JavaScript, $ may match before a final trailing newline even without m. Test with your real strings.

word

$ is not a word-boundary

For “ends a word but not necessarily the string,” use \b instead of $.

escape

Literal dollar needs escaping

To match a $ character in text, use /\$/. Unescaped $ is always the end anchor.

Browser & Runtime Support

The $ end anchor is part of core JavaScript regular expression syntax (ES3+).

✓ Baseline · ES3+

RegExp $ end anchor

Supported in every browser and Node.js version. Multiline $ with the m flag is equally universal.

99% Universal syntax
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
$ Excellent

Bottom line: Safe everywhere. Use $ when a pattern must finish at the end of a string or line.

Performance Notes

ApproachCostBest for
/suffix$/O(n)Suffix / extension checks
str.endsWith("x")O(k)Fixed literal suffixes (no regex needed)
/end$/gmO(n)Every line end in multi-line text

Anchors are cheap assertions. Prefer endsWith for a fixed literal; use $ when you need a pattern (extensions, optional parts, or case flags).

Key Takeaways

  • Role: $ asserts the end of the string (or line with m).
  • Zero-width: it matches a position, not a character.
  • Pair: use ^ + $ for a full-string match.
  • Context: pattern $ ≠ replacement $1.

One line: put $ at the end of a pattern when the match must finish at the end of the string or line.

Frequently Asked Questions

In a pattern, $ is a zero-width end anchor. It matches a position at the end of the input string, or—when the m (multiline) flag is set—immediately before each newline. It does not consume any characters.
No. In a pattern, $ asserts end of string or line. In a replacement string, $1 / $2 refer to capture groups and $$ is a literal dollar. The meaning depends on context.
Use $ with m when your text has newlines and you want the end of each line—not only the very last character of the whole string. Example: /end$/m matches end at the close of every line.
$ matches end of string or line (with m). \b matches a word boundary. /ing\b/ finds a word ending in ing; /ing$/ requires ing at the string or line end.
In JavaScript, $ can also match before a final trailing newline even without m. Prefer testing with your real input, and use m when you need every internal line end.
The $ anchor is core JavaScript regex syntax since ES3. It works in every browser and Node.js version.

Next: ?! negative lookahead

Learn how to assert that a pattern is not followed by something else.

?! tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful