'meaning of dollar symbol in regular expression

Can you help me what is the usage of $ in the following regular expression; I don't understand what is the usage? Does it mean just the end of string?

p.match(/^\.\.?($|\/)/)


Solution 1:[1]

Let's deconstruct your regex (I removed the backslashes that are used to escape characters for the sake of simplification, we will use the dots and slashes as literal here) so we're left with :

^..?($|/)

  • ^ means the beginning of a line
  • . then we must have a dot
  • .? then we may or may not have a second dot
  • $|/ and finally, we either end the line (that's what the $ sign does), or continue after a /

The parenthesis are used to return what's inside it in variables.

Your regex will detect the following strings :

..
../
./
../anytext
./anytext

Hope this helped.

Solution 2:[2]

To answer your question: yes, the $ in this regular expression means the end of string.

The following part:

($|\/)

means end of string or '/'.

In terms of string matching, this regular expression matches:

  • .
  • ..
  • Any string begin with './'
  • Any string begin with '../'

The first 2 strings are matched because of $, the last 2 patterns are matched because of /.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2