4

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,456
  • 5
  • 26
  • 41
  • 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

5

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

str.match(/\/$/);
maerics
  • 143,080
  • 41
  • 260
  • 285
3

You need to escape the slash:

str.match(/\/$/) 
Jukka K. Korpela
  • 187,417
  • 35
  • 254
  • 369
2

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

 str.match(/\/$/);
Ahmed Masud
  • 20,234
  • 3
  • 30
  • 56
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
  • 170,943
  • 22
  • 265
  • 283