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(/^\.\.?($|\/)/)

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.


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 /.


It doesn't mean anything special.

But because $ is allowed in identifier names, many Javascript libraries have taken to using $ as the "central" interface to them, or at least as a shortcut for accessing their functionality.

For example, if you're using jQuery and you say $("div"), this is a call to the $ function with argument "div". When you say $.post(), it's calling the post method on the $ object (Javascript is nice in that functions are first-class objects).