To check if string starts with “http”, use PHP built-in function strpos(). strpos() takes the string and substring as arguments and returns 0, if the string starts with “http”, else not.
Use the test() method to check if a regular expression matches an entire string, e.g. /^hello$/. test(str) . The caret ^ and dollar sign $ match the beginning and end of the string. The test method returns true if the regex matches the entire string, and false otherwise.
The Difference Between \s and \s+ For example, expression X+ matches one or more X characters. Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 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)://
Case insensitive:
var re = new RegExp("^(http|https)://", "i");
var str = "My String";
var match = re.test(str);
^https?://
You might have to escape the forward slashes though, depending on context.
^https?:\/\/(.*)
where (.*)
is match everything else after https://
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With