crankshaft
crankshaft

Reputation: 2677

javascript regexp to match path depth

Been struggling for the last hour to try and get this regexp to work but cannot seem to crack it.

It must be a regexp and I cannot use split etc as it is part of a bigger regexp that searches for numerous other strings using .test().

(public\/css.*[!\/]?)

public/css/somefile.css
public/css/somepath/somefile.css
public/css/somepath/anotherpath/somefile.css

Here I am trying to look for path starting with public/css followed by any character except for another forward slash.

so "public/css/somefile.css" should match but the other 2 should not.

A better solution may be to somehow specify the number of levels to match after the prefix using something like

(public\/css\/{1,2}.*) 

but I can't seem to figure that out either, some help with this would be appreciated.

edit

No idea why this question has been marked down twice, I have clearly stated the requirement with sample code and test cases and also attempted to solve the issue, why is it being marked down ?

Upvotes: 1

Views: 971

Answers (2)

Tushar
Tushar

Reputation: 87233

You can use this regex:

/^(public\/css\/[^\/]*?)$/gm
  1. ^ : Starts with
  2. [^/] : Not /
  3. *?: Any Characters
  4. $: Ends with
  5. g: Global Flag
  6. m: Multi-line Flag

Upvotes: 2

EJTH
EJTH

Reputation: 2219

Something like this?

/public\/css\/[^\/]+$/

This will match public/css/[Any characters except for /]$

$ is matching the end of the string in regex.

Upvotes: 1

Related Questions