Clare Barrington
Clare Barrington

Reputation: 1155

Regex - partial url

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})$

Upvotes: 1

Views: 312

Answers (3)

Bram Vanroy
Bram Vanroy

Reputation: 28554

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?))?$

Upvotes: 3

Cyrbil
Cyrbil

Reputation: 6478

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

Upvotes: 1

user4227915
user4227915

Reputation:

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.

Upvotes: 1

Related Questions