Decrease Font Size
Increase Font Size

Regular Expression to validate file path and extension

by bryian 24. June 2011 18:22

Regex that matches file path and extension |

Regular expression for full file path and file extension |

Using Regular Expressions to validate a filename and path|

Regular Expression to validate file path and extension


Recently I was looking for a regular expression to validate a file path and extension. I found several of them on Google but none of them fit my requirement. So I decided to put together a version that suits my need. Here is the Regular Expression to validate the file path and extension and it compatible with JavaScript and ASP.NET.



^(?:[\w]\:|\\) -- Begin with x:\ or \\

[a-z_\-\s0-9\.] -- valid characters are a-z| 0-9|-|.|_ (you can add more)

(txt|gif|pdf|doc|docx|xls|xlsx) -- Valid extension (you can add more)


\\\my folder\folder.2\file.gif
c:\my folder\abc abc.docx

\\\my folder\folder.2\.gif
c:\my folder\another_folder\.docx
c:\my folder\\another_folder\abc.docx
c:\my folder\another_folder\ab*c.v2.docx

Test it:

Comments are closed