Page 1 of 1
help me in this regular expression
Posted: Thu May 22, 2008 1:15 am
by eshban
i have a regular expression
var re = /^[\w][\w\.\-]{1,60}$/i;
it verify 4 validations
1) Uploading of files in this format is not allowed
2) file names follow this format (filename.extension)
3) Entire file cannot exceed 60 characters
4) The filename cannot contain any SPACES
1) How can i removed the last validation from this REGULAR EXPRESSION, means regular expression allows user to upload files with spaces in filename.
2) how can i change this regular expression, so that when user uploads any file with spaces in filename, then my code / regular expression will replace filename spaces with UNDERSCORE (_)
Remember this is a javascript REGULAR EXPRESSION
Thanks
Re: help me in this regular expression
Posted: Thu May 22, 2008 1:53 am
by prometheuzz
eshban wrote:i have a regular expression
var re = /^[\w][\w\.\-]{1,60}$/i;
The . (dot) and, in this case, the - (hyphen) do not need to be escaped inside the character class.
eshban wrote:it verify 4 validations
1) Uploading of files in this format is not allowed
2) file names follow this format (filename.extension)
That is not correct, your regex also matches strings that end with a . (dot) or - (hyphen).
eshban wrote:3) Entire file cannot exceed 60 characters
4) The filename cannot contain any SPACES
1) How can i removed the last validation from this REGULAR EXPRESSION, means regular expression allows user to upload files with spaces in filename.
Add a
\s to your character class.
eshban wrote:2) how can i change this regular expression, so that when user uploads any file with spaces in filename, then my code / regular expression will replace filename spaces with UNDERSCORE (_)
Remember this is a javascript REGULAR EXPRESSION
Thanks
Re: help me in this regular expression
Posted: Thu May 22, 2008 2:21 am
by eshban
what is /s, what is its effect
and can you please update my regular expression, so that it will automatically accepts filenames with spaces in it, and replace space with UNDERSCORE.
thanks
Re: help me in this regular expression
Posted: Thu May 22, 2008 2:26 am
by eshban
it works
thanks for your help
i am using this. And it automatically convert spaces into understand. Please review that is it ok?
Re: help me in this regular expression
Posted: Thu May 22, 2008 2:54 am
by prometheuzz
eshban wrote:it works
thanks for your help
i am using this. And it automatically convert spaces into understand.
I guess you mean underscore. And no, it does not automatically convert white spaces into them. You will first need to match the file name, and (when it is matched) then replace the white spaces with underscores. Something like this:
Code: Select all
var fileName = "a file name.txt";
if(fileName.match(/^\w[\w\s.+-]+$/gi)) {
fileName = fileName.replace(/\s/gi, "_");
document.write(fileName);
} else {
document.write("No match!");
}
I did not test it, nor am I very good in JS, so adjust accordingly!
eshban wrote:Please review that is it ok?
I don't know if it's ok for you. Like I said, your regex will also match strings like these:
a-filename.
another-filename-----------------------.......-..-.--..-.-.-.-.--
I don't know if you want them to be accepted or not.