End of string anchor $

Q

What is the "end of string" anchor in a regular expression? How to use it?

How to write a regular expression using "end of string" anchor to validate words ending in 'z': quartz?

✍: FYIcenter.com

A

The "end of string" anchor is an atomic zero-width assertion specifing that the match must occur at the end of the string. The "end of string" anchor is represented as '$'. For example:

\w+day$   # matches strings of 'Monday', 'Tuesday', etc.

The regular expression to validate words ending in 'z':

\w+z$

\w+ - matches word characters 1 or more times
z - matches 'z'
$ - matches 'end of string'

Click the button to test this regular expression here online:

2013-01-26, 0👍, 0💬