views:

30

answers:

1

Hi,

I am trying to come up with a JS regex that will include any intranet site (with the simple definition of any host name that does not include .), but will exclude localhost.

I've got the first part: http(s)?\:\/\/[^\.\/]+(\/.*)* I am struggling with the second part. Any help would be appreciated.

+3  A: 

You need to use a negative lookahead:

/^http(s)?\:\/\/(?!localhost[:\/])[^\.\/]+(\/.*)*$/
SLaks
Yep, that's pretty close. Though, it seems to wrongly match non-port-80 locahost URLs (`http://localhost:8080/`). Here's the one that works for that case too: `http(s)?\:\/\/(?!localhost)[^\.\/]+(\/.*)*`. Thanks!
Franci Penov
@Franci: That won't match `http://localhostother/`
SLaks
Correct. It will also wrongly match `http://francip@localhost/`. I decided I can live with these limitations for my purposes. :-)
Franci Penov