help me in this regular expression

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

help me in this regular expression

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: help me in this regular expression

Post 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.

Code: Select all

/^[\w][\w.-]{1,60}$/i
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

Code: Select all

string.replace(/\s/gi, _);
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Re: help me in this regular expression

Post 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
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Re: help me in this regular expression

Post by eshban »

it works
thanks for your help

Code: Select all

var re=  /^[\w][\w\s.-]{1,60}$/i;
i am using this. And it automatically convert spaces into understand. Please review that is it ok?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: help me in this regular expression

Post by prometheuzz »

eshban wrote:it works
thanks for your help

Code: Select all

var re=  /^[\w][\w\s.-]{1,60}$/i;
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.
Post Reply