1

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 ?

1
  • Using regex and split aren't really detrimental to one another. Commented Jun 9, 2015 at 11:39

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

Something like this?

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

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

$ is matching the end of the string in regex.

2 Comments

Thanks for helping, I just tried that on regex101 does not appear to work ( regex101.com/r/oI6nJ9/2 )
Yes it does... The site you use doesn't treat $ as line ending but end of file...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.