Forum Moderators: open

Message Too Old, No Replies

Reg Ex JScript again!

URL validation

         

benji

5:31 pm on Sep 23, 2008 (gmt 0)

10+ Year Member



I currently have the following RegExp for URL validation:

(ftp¦http¦https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/¦\/([\w#!:.?+=&%@!\-\/]))?/;

It works OK, but I want to be able to give the option of not having to enter http, or ftp etc, so would start with www.
Also, I would like it to accept both lower and upper case - I know the flag for this is the letter "i", but can't seem to integrate this into the RegExp.
Help would be most appreciated.

phranque

8:30 pm on Sep 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



to make the protocol optional, start the regexp with:
((ftp多ttp多ttps):)?\/\/ ...

benji

9:19 pm on Sep 23, 2008 (gmt 0)

10+ Year Member



Thanks for the reply, but I tried it and doesn't seem to work.. I origionally thought to try starting with:

((ftp多ttp多ttps):\/\/)?...

Thinking this would enable the http/ftp:// optional, thus enabling just www. or [www....] and so on.

What does work is:

(ftp多ttp多ttps)?:\/\/)

But this only allows for the http/ftp bit to be optional, so you would still have to enter ://www

PS: glad I have actually found a friendly useful site :)

phranque

11:22 pm on Sep 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



((ftp多ttp多ttps):\/\/)?...

that's actually what i meant to type and i'm not sure why it doesn't work.

have you tried printing out the strings captured by the backreferences.
also, try putting a backslash before the colon and see if that fixes things.

glad I have actually found a friendly useful site

welcome to WebmasterWorld [webmasterworld.com], benji!
(sorry i didn't notice that!)

Dabrowski

5:40 pm on Sep 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(ftp多ttp多ttps):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/吒/([\w#!:.?+=&%@!\-\/]))?/;

That is a good regex! I agree with Phranque, that should make the protocol section optional.

I assume you know that a ? in a regex means 0 or 1 times, so it's a bit odd using :{0,1} to check for a colon in the user/pass part. Would this be better?

(\w+(:\w+)?@)?

Also,

(\S+)
would pass an invalid URL, \S is any character that's not whitespace. You might want to use something like this, which has the advantage of grouping the domain and port number into $5.
(\w+(\.\w+)+(:[0-9]+)?)?

If you're struggling with regexes, or even have a simple one you want to mess with, I find this program EXTREMELY useful:
[weitz.de...]

It's written for Perl but simple matches are the same for JavaScript.

Infact, I've just checked and

((ftp多ttp多ttps):\/\/)?
does validate without the protocol section.

With the

(\/吒/([\w#!:.?+=&%@!\-\/]))?
section on the end the regex fails to validate the URL of this page.

benji

4:53 pm on Sep 29, 2008 (gmt 0)

10+ Year Member



Once again - thankyou all. Very, very helpful.