Regex to test if string begins with http:// or https://

Regex

Regex Problem Overview


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)]://

Regex Solutions


Solution 1 - Regex

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)://

Solution 2 - Regex

Case insensitive:

var re = new RegExp("^(http|https)://", "i");
var str = "My String";
var match = re.test(str);

Solution 3 - Regex

^https?://

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

Solution 4 - Regex

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

Solution 5 - Regex

This should work

^(http|https)://

Solution 6 - Regex

^ 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

Solution 7 - Regex

(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

Solution 8 - Regex

This will work for URL encoded strings too.

^(https?)(:\/\/|(\%3A%2F%2F))

Solution 9 - Regex

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAliView Question on Stackoverflow
Solution 1 - RegexcdhowieView Answer on Stackoverflow
Solution 2 - RegexmishapView Answer on Stackoverflow
Solution 3 - RegexorlpView Answer on Stackoverflow
Solution 4 - RegexSerhii AksiutinView Answer on Stackoverflow
Solution 5 - RegexTasawer KhanView Answer on Stackoverflow
Solution 6 - RegexSridharKrithaView Answer on Stackoverflow
Solution 7 - RegexLorenzo LeuckView Answer on Stackoverflow
Solution 8 - RegexSaravananView Answer on Stackoverflow
Solution 9 - RegexTony L.View Answer on Stackoverflow