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 ?

7
  • 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. Commented Oct 30, 2015 at 11:41
  • Try this simple regex /\w+:?\w*\.jpe?g/i Commented Oct 30, 2015 at 11:42
  • Possible duplicate of Regex JavaScript image file extension Commented Oct 30, 2015 at 11:44
  • 1
    I dont see this regex even correct Commented Oct 30, 2015 at 11:49
  • 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. Commented Oct 30, 2015 at 12:07

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

with this expression, PjjeJJEEEEpJjPEEEGgpjEjE?gg is a valid extension
@valepu What? No, that is not a valid expression.
no it's not a valid expression but it's a valid extension for the file (that is, the part after the dot)
again, i said extension (the part after the dot) Try a:b.PjjeJJEEEEpJjPEEEGgpjEjE?gg,
you can see here 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
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!

Comments

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.