Anyone want to try their hand at coming up with a regex that matches both:
I
I used naming capture groups. We get better matches when the scheme is present. Like www.foo.com/bar would only match /bar.
(?:
(?:(?https?|file)://)
(?[^/]+)
(?/(?:[^\s])+)?
)
|
(?/(?:[^\s])+)
This is what you could do for javascript
var result = text.match(/(?:(?:(https?|file):\/\/)([^\/]+)(\/(?:[^\s])+)?)|(\/(?:[^\s])+)/g);
Test data
sadfasdf /foo/bar/ba090z.gif asdfasdf /foo/bar/ sadfasdf asdflkj; http://www.foo.com/foo/bar some stuff http://user:pw@www.foo.com:80/r?stuff%20stuff
user:pw@www.foo.com:80/r?stuff%20stuff