I’m trying to understand this javascript regular expression.
Can anyone give me an explanation about this regexp parts?
location.href.match(/^http:\/\/(www\.)?example.com/i)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
lets break it up in bits:
^http:\/\/= the string has to begin withhttp://. The backslashes are there because if they wouldn’t, the slashes would end the regex pattern.(www\.)?= matcheswww.if it exists (that’s what the question mark is for)example.com= string must be followed withexample.comi= case insensitiveSo these are possible matches:
http://example.comhttp://www.example.comhttp://www.EXAMPLE.COMhttp://www.example.com/some/page/Unfortunately, the regex wouldn’t match the HTTPS protocol. We can use the same method applied to
www.using the question mark:/^http(s)?:\/\/(www\.)?example.com/i