Regex to test if string begins with http:// or https://
Solution 1:
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:
Case insensitive:
var re = new RegExp("^(http|https)://", "i");
var str = "My String";
var match = re.test(str);
Solution 3:
^https?://
You might have to escape the forward slashes though, depending on context.
Solution 4:
^https?:\/\/(.*)
where (.*)
is match everything else after https://
Solution 5:
This should work
^(http|https)://