245

I'm trying to set a regexp which will check the start of a string, and if it contains either http:// or https:// it should match it.

How can I do that? I'm trying the following which isn't working:

^[(http)(https)]://
moinudin
  • 134,091
  • 45
  • 190
  • 216
Ali
  • 261,656
  • 265
  • 575
  • 769
  • 5
    If you're checking just the start of the string, it's probably faster to just do a straight comparison of the first few characters of the string with the patterns you're looking for. – templatetypedef Jan 10 '11 at 02:03
  • 3
    You are creating a *character group* with `[]`. It will mach **one** character that is either `(`,`)`,`h`,`t`,`t`,`p` or `s`. I.e. it would match `s://` but not `ht://` or `x://`. – Felix Kling Jan 10 '11 at 02:05
  • 2
    @templatetypedef: I think I sense some premature optimization. – cdhowie Jan 10 '11 at 02:20
  • 4
    Many modern regular expression libraries are *very fast*. Unless there is (lots of) back-tracking, regular expressions may compare favorably -- or better -- to "index-of" style approaches (compare `/^x/` vs `indexOf(x) == 0`). "starts with" style approaches may have less overhead, but I suspect it rarely matters -- choose what is the cleanest, which very well may be: `x.StartWith("http://") || x.StartsWith("https://")` -- but do so out of code clarity, not an attempt to improve performance unless justified with analysis and requirements :-) –  Jan 10 '11 at 02:42

9 Answers9

433

Your use of [] is incorrect -- note that [] denotes a character class and will therefore only ever match one character. The expression [(http)(https)] translates to "match a (, an h, a t, a t, a p, a ), or an s." (Duplicate characters are ignored.)

Try this:

^https?://

If you really want to use alternation, use this syntax instead:

^(http|https)://
JakeTheSnake
  • 2,456
  • 3
  • 14
  • 26
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 2
    As a PHP input string: $regex = '/^(https?:\/\/)'; – Steve Tauber Jul 28 '14 at 14:09
  • 12
    Steve, I think you missed a / at the end: $regex = '/^(https?:\/\/)/'; – Axi May 19 '15 at 15:03
  • 16
    Just in case some nut accidentally uppercases the http, $regex = '/^(https?:\/\/)/i'; – jeffkee Jan 08 '16 at 23:19
  • 3
    You forgot to escape / using \. So it would be `^https?:\/\/`. Am I right? – Shafizadeh Jan 22 '16 at 23:30
  • 4
    @Shafizadeh `/` is not a special character in regular expressions, only in languages where `/` is used to notate a literal regular expression. For example, it is not necessary to escape `/` in regular expressions when using C#, because C# regular expressions are expressed (in part) as string literals. Nor do you need them in, say, Perl (when using an alternate delimiter as in `m#^https?://#`). So to directly address your comment: (a) No, I did not forget to escape anything. (b) You will need to escape whatever characters are treated specially in your language of choice. – cdhowie Jan 23 '16 at 02:05
  • @Shafizadeh As another counter-example, when using `grep` from the command-line, the `/` characters do not need to be escaped, but *the `?` character does!* This answer is intended to be language-neutral. – cdhowie Jan 23 '16 at 02:08
  • This url will validate as successful https:////m.files.bbci.co.uk/modules/bbc-morph-sport-seo-meta/1.23.3/images/bbc-sport-logo.png. If using PHP i think https://www.w3schools.com/PHP/filter_validate_url.asp is the way to go. – Daniel Aug 30 '23 at 12:12
  • Also maybe ```^(https?:\/\/[\w\d])``` – Daniel Aug 30 '23 at 12:34
51

Case insensitive:

var re = new RegExp("^(http|https)://", "i");
var str = "My String";
var match = re.test(str);
mishap
  • 8,176
  • 14
  • 61
  • 92
32
^https?://

You might have to escape the forward slashes though, depending on context.

orlp
  • 112,504
  • 36
  • 218
  • 315
32

^https?:\/\/(.*) where (.*) is match everything else after https://

Serhii Aksiutin
  • 619
  • 6
  • 7
19

This should work

^(http|https)://
Tasawer Khan
  • 5,994
  • 7
  • 46
  • 69
2

^ for start of the string pattern,

? for allowing 0 or 1 time repeat. ie., s? s can exist 1 time or no need to exist at all.

/ is a special character in regex so it needs to be escaped by a backslash \/

/^https?:\/\//.test('https://www.bbc.co.uk/sport/cricket'); // true

/^https?:\/\//.test('http://www.bbc.co.uk/sport/cricket'); // true

/^https?:\/\//.test('ftp://www.bbc.co.uk/sport/cricket'); // false
SridharKritha
  • 8,481
  • 2
  • 52
  • 43
1

(http|https)?:\/\/(\S+)

This works for me

Not a regex specialist, but i will try to explain the awnser.

(http|https) : Parenthesis indicates a capture group, "I" a OR statement.

\/\/ : "\" allows special characters, such as "/"

(\S+) : Anything that is not whitespace until the next whitespace

0

This will work for URL encoded strings too.

^(https?)(:\/\/|(\%3A%2F%2F))
Saravanan
  • 115
  • 2
  • 15
-1

Making this case insensitive wasn't working in asp.net so I just specified each of the letters.

Here's what I had to do to get it working in an asp.net RegularExpressionValidator:

[Hh][Tt][Tt][Pp][Ss]?://(.*)

Notes:

  • (?i) and using /whatever/i didn't work probably because javascript hasn't brought in all case sensitive functionality
  • Originally had ^ at beginning but it didn't matter, but the (.*) did (Expression didn't work without (.*) but did work without ^)
  • Didn't need to escape the // though might be a good idea.

Here's the full RegularExpressionValidator if you need it:

<asp:RegularExpressionValidator ID="revURLHeaderEdit" runat="server" 
    ControlToValidate="txtURLHeaderEdit" 
    ValidationExpression="[Hh][Tt][Tt][Pp][Ss]?://(.*)"
    ErrorMessage="URL should begin with http:// or https://" >
</asp:RegularExpressionValidator>
Tony L.
  • 17,638
  • 8
  • 69
  • 66