End of string

2 snippets in Regular Expressions

RXRegular Expressions

$ End of String

RX · Basic Patterns
Syntax
pattern$
Example
JS:  /\.json$/.test('config.json')   // true
JS:  /\.json$/.test('config.json.bak')  // false
Py:  bool(re.search(r'\.json$', 'config.json'))  # True
Output
true, false, True

Note Matches position at end of string. With the m flag, $ matches the end of each line. In Python, re.match only checks the start -- use re.search with $ to check the end.

$ End Anchor

RX · Anchors & Boundaries
Syntax
pattern$
Example
JS:  /\.py$/.test('script.py')       // true
JS:  /\.py$/.test('script.py.bak')   // false
Py:  bool(re.search(r'\.py$', 'main.py'))  # True
Output
true, false, True

Note Without the m flag, $ matches the very end of the string (or before a trailing newline in Python). With m, it matches the end of each line.

Frequently asked questions

How does Regular Expressions handle end of string?
Regular Expressions covers this with 2 copy-ready snippets on this page. The "$ End of String" snippet in Regular Expressions uses `pattern$`.
Which pattern does the Regular Expressions example use?
The "$ End of String" snippet uses `pattern$`, from the Basic Patterns section of the Regular Expressions cheat sheet.
What other Regular Expressions snippets are shown for "end of string"?
Besides "$ End of String", this page also shows "$ End Anchor".
Is there anything to watch out for?
Yes. For "$ End of String": Matches position at end of string. With the m flag, $ matches the end of each line. In Python, re.match only checks the start -- use re.search with $ to check the end.