Course contentsShow
← JavaScript
Lesson 1691 of 2,72336. Regular ExpressionsFree lesson

String Start and End: \A and \Z (Not in JS)

Clarify that JavaScript lacks \A and \Z; ^ and $ serve this role in non-multiline mode.

String Start and End: \A and \Z (Not in JS)

You'll learn why JavaScript doesn't have \A and \Z anchors, and what to use instead.

The Concept

In some regex engines (like Ruby, Perl, and Python), \A and \Z are special anchors that match the absolute start and end of the entire string, regardless of multiline mode. They're different from ^ and $, which can change behavior when the multiline flag is set.

JavaScript doesn't support \A and \Z at all. If you try to use them, they'll be treated as literal characters (or cause errors), not as anchors.

What JavaScript Uses Instead

In JavaScript, ^ and $ serve a dual purpose:

  • Without the m flag: ^ matches the start of the string, $ matches the end of the string (exactly what \A and \Z do in other languages)
  • With the m flag: ^ matches the start of any line, $ matches the end of any line

The Bottom Line

If you're reading regex documentation from other languages or porting patterns to JavaScript, remember:

  • Replace \A with ^
  • Replace \Z with $
  • Just ensure you're not using the multiline flag if you want strict string-boundary matching

Think of it this way: JavaScript kept things simpler by making ^ and $ context-aware based on the m flag, rather than having separate anchors for string boundaries versus line boundaries.

Key Takeaway: JavaScript doesn't have \A or \Z anchors; use ^ and $ without the multiline flag to match the absolute start and end of a string.