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
help me in this regular expression
Moderator: General Moderators
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: help me in this regular expression
The . (dot) and, in this case, the - (hyphen) do not need to be escaped inside the character class.eshban wrote:i have a regular expression
var re = /^[\w][\w\.\-]{1,60}$/i;
Code: Select all
/^[\w][\w.-]{1,60}$/iThat is not correct, your regex also matches strings that end with a . (dot) or - (hyphen).eshban wrote:it verify 4 validations
1) Uploading of files in this format is not allowed
2) file names follow this format (filename.extension)
Add a \s to your character class.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.
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
Code: Select all
string.replace(/\s/gi, _);Re: help me in this regular expression
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
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
it works
thanks for your help
i am using this. And it automatically convert spaces into understand. Please review that is it ok?
thanks for your help
Code: Select all
var re= /^[\w][\w\s.-]{1,60}$/i;- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: help me in this regular expression
eshban wrote:it works
thanks for your help
i am using this. And it automatically convert spaces into understand.Code: Select all
var re= /^[\w][\w\s.-]{1,60}$/i;
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 don't know if it's ok for you. Like I said, your regex will also match strings like these:eshban wrote:Please review that is it ok?
a-filename.
another-filename-----------------------.......-..-.--..-.-.-.-.--
I don't know if you want them to be accepted or not.