16
8
Create the shortest regular expression that will roughly match a URL in text when run in JavaScript
Example:
"some text exampley.com".match(/your regular expression goes here/);
The regular expression needs to
- capture all valid URLS that are for http and https.
- not worry about not matching for URL looking strings that aren't actually valid URLS like
super.awesome/cool
- be valid when run as a JavaScript regex
Test criteria:
Match:
- http://example.com
- http://example.com/
- http://example.com/super
- https://example.com/super
- example.com/super
- example.com
- example.com/su-per_duper/?add=yes&subtract=no
- example.com/archive/index.html
- twitter.com/#!/reply
- example.com/234ret2398oent/234nth
- codegolf.stackexchange.com/questions/464
- crazy.wow.really.example.com/?cat=nth%3E
- example-example.com
- example1.com
Not Match:
- example
- super/cool
- Good Morning
- i:can
- hello.
Here is a test that might help clarify a bit http://jsfiddle.net/MikeGrace/gsJyr/
I apologize for the lack of clarity, I hadn't realized how awful matching URLs was.
Ahgrrrr! I miss my edit privileges! I you're going to restrict the game to one language perhaps you should tag it with that language. – dmckee --- ex-moderator kitten – 2011-02-04T05:25:12.763
What constitute a valid URL character? because I can simply use
\w
for everything Do you expect backreferences for different URL components? – Ming-Tang – 2011-02-04T05:49:13.0131
"A URI is a sequence of characters from a very limited set, i.e. the letters of the basic Latin alphabet, digits, and a few special characters," according to RFC 2396.
– RunnerRick – 2011-02-04T06:18:17.207Mike: I guess there is still some clarification in order. As it stands now I can just use
/:/
as the regular expression and match valid URIs and not match all your examples on the »Not match« list. As long as you're going that route it's simply the question: What is the shortest regular expression that will not match any of the example strings but still catch all URIs. – Joey – 2011-02-04T09:10:44.620I think this questions seems to be a "give me teh codez" question. – None – 2011-02-04T17:32:07.670
@M28 the lack of clarity may seem that way but I did learn a lot from it and I'm still working on my own answer. If you think it should be deleted we can do that if it is better for the community. – Mike Grace – 2011-02-04T18:15:03.510
1Just try to write a longer challenge with more details. – None – 2011-02-04T22:06:58.420