PDA

View Full Version : Regular Expression for directory path



swiji2000
2 Jul 2013, 1:07 AM
Hi

Here i am writing the code for validating file path.

var path="D:\\filepath\";
var regex='^([\w]\:(\\\\|\\)([a-zA-Z0-9\-\_\\]+))$';
if (!path.match(regex)){
Ext.Msg.alert('Message','Please enter correct file structure.');
return;
}

It was not working and i am getting the following error.

"Uncaught SyntaxError: Invalid regular expression: + Unterminated character class"

Kindly help me to resolve this issue.

Thanks in advance

swiji2000
2 Jul 2013, 1:10 AM
I checked with
"/^([\w]\: (\\\\|\\) ([a-zA-Z0-9\-\_\\]+))$/" regex also.

slemmon
3 Jul 2013, 1:02 PM
I'm not all that strong with regex, but here is an example for file types and a resource that is great for building and testing regex:



regex: /(.)+((\.csv)|(\.xls)(\w)?)$/i

In this case it'll accept .csv or .xls (as well as any character after .xls such as .xlsx). Additinally it won't care whether the path is included or not - just looks for SOMETHING plus the approved file extension.


http://gskinner.com/RegExr/