5

The regular expression for a string that ends with '/' is the following:

str.match(//$/) -- javascript syntax

but the // makes the compiler think it's a comment. how to work around this?

Laguna
  • 3,706
  • 5
  • 27
  • 42
  • 1
    what about this : http://stackoverflow.com/questions/1569295/regex-to-remove-javascript-double-slash-style-comments – Afshin Jan 12 '12 at 21:12
  • Oh god, so many people typing the same thing but no one checking their answer first! – Gareth Jan 12 '12 at 21:13

4 Answers4

6

You must escape the final / so the interpreter doesn't think it terminates the RegExp literal:

str.match(/\/$/);
maerics
  • 151,642
  • 46
  • 269
  • 291
3

You need to escape the slash:

str.match(/\/$/) 
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
2

Use the escape character (\) to specify a literal / as in:

 str.match(/\/$/);
Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
2

You'll need to escape the slash

str.match(/\/$/);

If you want to match a string that ends with slash, you may want to include the actual string too;

str.match(/.*\/$/);
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • " you may want to include the actual string too" <-- no need at all. If you want to see if an input ends with a /, you only have to look for a / followed by the end of input ($). – fge Jan 12 '12 at 21:31
  • Yes, but if you want to use the actual strings returned by the matches, they're not very useful without it. – Joachim Isaksson Jan 12 '12 at 21:33