skip to Main Content

It says that:

$ specifies a match at the end of a string

So why then is a match returned for c$ in the string abc? If c is the last character of the string, shouldn’t it be looking for what comes after c and not c itself, and therefore return null?

2

Answers


  1. No. $ means the end of the string, not any characters before or after the end. Think of it as the boundary after the last character. c$ will match "abc" but not "cba".

    Login or Signup to reply.
  2. So why then is a match returned for c$ in the string abc? If c is the last character of the string, shouldn’t it be looking for what comes after c and not c itself, and therefore return null?

    /c$/ will match any string ending with 'c', for example: 'c', 'abc', '1234c' but will not match 'c1','c '..

    Similarly, you have ^ which indicates a beginning of the string.
    /^c/ will match any string starting with 'c', for example: 'c', 'cab', 'crab'.. but will not match '1c', ' c'..

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search