Create the shortest regular expression that will roughly match a URL in text when run in JavaScript


"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:


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.

  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – dmckee --- ex-moderator kitten Feb 4 '11 at 5:25
  • 1
    \$\begingroup\$ "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. \$\endgroup\$ – RunnerRick Feb 4 '11 at 6:18
  • 1
    \$\begingroup\$ Just try to write a longer challenge with more details. \$\endgroup\$ – user11 Feb 4 '11 at 22:06
  • 1
    \$\begingroup\$ Voting to close as there is no specification of what you count as a URL. Should we allow usernames/passwords in the URL? Ports? Should we validate URL lengths? \$\endgroup\$ – Redwolf Programs Nov 11 '20 at 15:29
  • 1
    \$\begingroup\$ I agree with Redwolf, in that this challenge requires a full spec of what is and isn't a valid URL, rather than that being defined through the test cases. \$\endgroup\$ – caird coinheringaahing Nov 11 '20 at 15:52

doesn't match 3 strings that it shouldn't, matches almost anything else ;)
upd: it still doesn't match all 5


This one works:

var re = /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi;

(^|\s)                            : ensure that we are not matching an url 
                                    embeded in an other string
(https?:\/\/)?                    : the http or https schemes (optional)
[\w-]+(\.[\w-]+)+\.?              : domain name with at least two components;
                                    allows a trailing dot
(:\d+)?                           : the port (optional)
(\/\S*)?                          : the path (optional)

Passes the tests at http://jsfiddle.net/9BYdp/1/

Also matches:

  • example.com. (trailing dot)
  • example.com:8080 (port)
  • \$\begingroup\$ Sweetness!!!!!!! \$\endgroup\$ – Mike Grace Feb 4 '11 at 9:33
  • 2
    \$\begingroup\$ Wouldn't you want to match a hostname with only one component as well (e.g. localhost)? \$\endgroup\$ – RunnerRick Feb 4 '11 at 17:20
  • \$\begingroup\$ This allows spaces \$\endgroup\$ – brenjt Nov 6 '13 at 17:52
  • \$\begingroup\$ works for me. ty :) \$\endgroup\$ – STEEL Feb 12 '16 at 11:20
  • \$\begingroup\$ Works nice, but not for domains with user/password parts e.g. http://user:password@domain.com/path \$\endgroup\$ – Radon8472 Aug 14 '18 at 12:55

This obviously doesn't do what you intend, but it meets your criteria:

  • "match all valid URLS that are for http and https."

    yep, definately will match.

  • "not worry about not matching for URL looking strings that aren't actually valid URLS like 'super.awesome/cool'"

    yeah, sure, there will be lots of false positives, but you said that doesn't matter.

  • be valid when run as a JavaScript regex

    sure as eggs works as you say it should.

If this result is NOT a right answer, then you need to be more selective with your criteria.

In order to be a rule that works as you intend, you actually do need to implement a full RFC compliant matcher, and a full RFC compliant matcher will "worry about not matching".

So, in terms of "permit not matching", you need to specify exactly which deviations from RFC are permissible.

Anything else, and this whole exercise is a sham, because people will just write whatever works for them, or how they like it, and sacrifice "making any sense" in favour of being short ( like I did ).

On your update

The most Naïve regex I can come up with that matches (and captures) all your pasted examples so far is:


Its quite simple in nature, and assumes only 3 basic forms are possible.


z can be anthing not whitespace. x can be anything not whitespace. y can be anything that is neither whitespace or a '/' character.

There are a lot of things that will be valid to this rule, lots, but they'll at least look like a valid URI to a human, they just won't be specifications compatible.


hello.0/1  # valid 
1.2/1 # valid 
muffins://¥.µ/€  # probably valid

I think the sane approach is to extract things that are likely to be URI's, then validate them with something stricter, I'm looking at working out how to use the browsers URI class to validate them =).

But you can see the above reasoning working on this sample here: http://jsfiddle.net/mHbXx/

  • \$\begingroup\$ He changed the question, but you can do better anyway with /:/ even after the edit :-) \$\endgroup\$ – Joey Feb 4 '11 at 9:11
  • \$\begingroup\$ Thanks Mike =). I don't wish to compete myself in a more serious manner, the other suggestions are more useful, I just wished to point out the problem with the initial premise so that the question quality could improve =) \$\endgroup\$ – Kent Fredric Feb 6 '11 at 5:02
  • \$\begingroup\$ Is it only me or is this matching "www .google .com"? \$\endgroup\$ – Schiavini Aug 23 '12 at 14:36

Try that.

I'm including the leading and trailing slashes that delimit the regular expression, so hopefully that doesn't hurt my character count!

This pattern limits the protocol to either http or https, allows for an optional port number, and then allows any character except whitespace.


Not the answer you're looking for? Browse other questions tagged or ask your own question.