1

While trying to submit a form a javascript regex validation always proves to be false for a string.

Regex:- ^(([a-zA-Z]:)|(\\\\{2}\\w+)\\$?)(\\\\(\\w[\\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$

I have tried following strings against it

abc.jpg,
abc:.jpg,
a:.jpg,
a:asdas.jpg,

What string could possible match this regex ?

Let me see
  • 5,063
  • 9
  • 34
  • 47
  • This question sounds too off-topic. You should replace all double backslashes with single ones in online regex testers and I think you need to escape `.` before `jpeg` and the rest, or better re-write the regex completely. – Wiktor Stribiżew Oct 30 '15 at 11:41
  • Try this simple regex [`/\w+:?\w*\.jpe?g/i`](https://regex101.com/r/eV8pH7/1) – Tushar Oct 30 '15 at 11:42
  • Possible duplicate of [Regex JavaScript image file extension](http://stackoverflow.com/questions/10473185/regex-javascript-image-file-extension) – IlGala Oct 30 '15 at 11:44
  • Due to some reasons I do not have to modify the regex. Just find a string matching this regex – Let me see Oct 30 '15 at 11:45
  • 1
    I dont see this regex even correct – Tushar Gupta Oct 30 '15 at 11:49
  • i think there are too many "\" they are escaping those that aren't supposed to be escaped (such as \w) – valepu Oct 30 '15 at 12:06
  • 1
    Its valid in its unescaped form (or passed as a string constructor) and would match a path like `c:\aa.jpeg`. Heaven forbid a .Jpeg - it should probably be rewritten. – Alex K. Oct 30 '15 at 12:07

3 Answers3

1

This regex won't match against anything because of that $? in the middle of the string.
Apparently using the optional modifier ? on the end string symbol $ is not correct (if you paste it on https://regex101.com/ it will give you an error indeed). If the javascript parser ignores the error and keeps the regex as it is this still means you are going to match an end string in the middle of a string which is supposed to continue.
Unescaped it was supposed to match a \$ (dollar symbol) but as it is written it won't work.

If you want your string to be accepted at any cost you can probably use Firebug or a similar developer tool and edit the string inside the javascript code (this, assuming there's no server side check too and assuming it's not wrong aswell). If you ignore the $? then a matching string will be \\\\w\\\\ww.jpg (but since the . is unescaped even \\\\w\\\\ww%jpg is a match)

Of course, I wrote this answer assuming the escaping is indeed the one you showed in the question. If you need to find a matching pattern for the correctly escaped one ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(\.jpeg|\.JPEG|\.jpg|\.JPG)$ then you can use this tool to find one http://fent.github.io/randexp.js/ (though it will find weird matches). A matching pattern is c:\zz.jpg

valepu
  • 3,136
  • 7
  • 36
  • 67
0

If you remove escape level, the actual regex is

^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$

After ^start the first pipe (([a-zA-Z]:)|(\\{2}\w+)\$?) which matches an alpha followed by a colon or two backslashes followed by one or more word characters, followed by an optional literal $. There is some needless parenthesis used inside.

The second part (\\(\w[\w].*))+ matches a backslash, followed by two word characters \w[\w] which looks weird because it's equivalent to \w\w (don't need a character class for second \w). Followed by any amount of any character. This whole thing one or more times.

In the last part (.jpeg|.JPEG|.jpg|.JPG) one probably forgot to escape the dot for matching a literal. \. should be used. This part can be reduced to \.(JPE?G|jpe?g).

It would match something like

A:\12anything.JPEG

\\1$\anything.jpg

Play with it at regex101. A better readable could be

^([a-zA-Z]:|\\{2}\w+\$?)(\\\w{2}.*)+\.(jpe?g|JPE?G)$

Also read the explanation on regex101 to understand any pattern, it's helpful!

Community
  • 1
  • 1
0

If you are just looking for a regular expression to match what you got there, go ahead and test this out:

(\w+:?\w*\.[jpe?gJPE?G]+,)

That should match exactly what you are looking for. Remove the optional comma at the end if you feel like it, of course.

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
  • with this expression, `PjjeJJEEEEpJjPEEEGgpjEjE?gg` is a valid extension – valepu Oct 30 '15 at 15:43
  • @valepu What? No, that is not a valid expression. – MortenMoulder Oct 30 '15 at 15:52
  • no it's not a valid expression but it's a valid **extension** for the file (that is, the part after the dot) – valepu Oct 30 '15 at 16:01
  • again, i said **extension** (the part after the dot) Try `a:b.PjjeJJEEEEpJjPEEEGgpjEjE?gg,` – valepu Oct 30 '15 at 16:51
  • you can see here http://fent.github.io/randexp.js/ some random strings which your regex will match against, you will see they are quite diffent than what you'd expect to match with the question's regex (even correctly escaped), and also quite different than a typical image filename – valepu Oct 30 '15 at 16:53