1

I think I’m pretty close, I’m writing a regex for a partial url.

Please note: my examples show a maximum of two / however it could have else or more. Example /test/test/test.htm

It can accept a-z 0-9 - and . if it has one of the file extensions referenced below. It can't start/end with a - or a . there must be a number or character before and after. Currently my regex is accepting strings which should be rejected

Accepted

/test/test.htm (this could be jpeg|jpg|gif|png|htm|html)
/test/test
/test/test-test.htm
/test/test-test
/test/test123.htm
/test/test123

Should be rejected (but passing)

/test/test.
/test/.hhh
/tes  t
/tes_t
/tes"t
/tes’t
/-test (cannot start with any thing else other than letters/numbers

Regex: ^\/.*?(\.(jpeg|jpg|gif|png|htm|html)|([^\.])[\w-]{1})$

4
  • 1
    Any case that is missing? Commented Dec 16, 2015 at 11:23
  • I think Ive thought of everything Commented Dec 16, 2015 at 11:26
  • 1
    Then what is your question? Commented Dec 16, 2015 at 11:26
  • 1
    What am I doing wrong, I need to restrict it Commented Dec 16, 2015 at 11:28

3 Answers 3

3

This is the most complete regex I could find. I've added a comment to others' regex, because they'd fail on /test/test- (which their regex would accept).

^\/[a-zA-Z0-9]+([-\/](?:[a-zA-Z0-9]+))*(\.(?:jpe?g|gif|png|html?))?$

See here.

Regex explanation

If you need to match consequent - as well (e.g. /test--test), you can use the following regex, as seen here.

^\/[a-zA-Z0-9]+((?:-+|\/)(?:[a-zA-Z0-9]+))*(\.(?:jpe?g|gif|png|html?))?$
Sign up to request clarification or add additional context in comments.

1 Comment

@WashingtonGuedes Not sure if that's a case OP needs to match, but I've added the regex for such a case.
1

Try this regex:

^(?:\/[a-z0-9](?:[^\/ _"’\n.-]|\.(?=(?:jpe?g|gif|png|html?)$)|\-(?!$))+)+$

Regex live here.

Explaining:

^(?: # from start \/[a-z0-9] # one slash and one letter or digit (?: # one of: [^\/ _"’\n.-] # characters not in this list | # OR \.(?=(?:jpe?g|gif|png|html?)$) # one dot with the condition # of being the extension dot # at the end | # OR \-(?!$) # one - not at the end )+ # at least one of them to many )+$ # as there could be /folder/folder # as many matches till the end

Hope it helps.

1 Comment

This will fail on /test/test- - which it'll match.
1

Can probably be optimized:

^(\/[a-zA-Z0-9\d]+)+([a-zA-Z0-9-]*\.(jpeg|jpg|gif|png|htm|html))?$

Try it live here

  • We first match pattern for folders like /abd with \/[a-zA-Z0-9\d]+ multiple time, that also include filename
  • Also allows filename to have a - in their name
  • Then optionally match for extension

4 Comments

Sorry probably me being stupid. /tes’t shows as matching, would that pass or fail?
Oups, forgot the $ to be more strict on the matching. Have a look at my last edit.
This will fail on /test/test- - which it'll match.
Edited by moving the extension parameter to include full filename.

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.