Laguna
Laguna

Reputation: 3876

Regular expression for a string that ends with '/'

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?

Upvotes: 5

Views: 8883

Answers (4)

Joachim Isaksson
Joachim Isaksson

Reputation: 181077

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(/.*\/$/);

Upvotes: 2

Ahmed Masud
Ahmed Masud

Reputation: 22412

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

 str.match(/\/$/);

Upvotes: 2

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201866

You need to escape the slash:

str.match(/\/$/) 

Upvotes: 3

maerics
maerics

Reputation: 156642

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

str.match(/\/$/);

Upvotes: 6

Related Questions