Reputation: 1155
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
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.
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
Reputation: 6478
Can probably be optimized:
^(\/[a-zA-Z0-9\d]+)+([a-zA-Z0-9-]*\.(jpeg|jpg|gif|png|htm|html))?$
/abd
with \/[a-zA-Z0-9\d]+
multiple time, that also include filename-
in their nameUpvotes: 1
Reputation:
Try this regex:
^(?:\/[a-z0-9](?:[^\/ _"’\n.-]|\.(?=(?:jpe?g|gif|png|html?)$)|\-(?!$))+)+$
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