This page is for formatting regexes using an older interface. It will have a single field for the regex, not the 3-field version with subdomain, tld and path.
HTTPS and WWW
Since some URLs start with http, and others with https, and some of your visitors may have a browser extension that automatically turns all http URLs into https ones, it's occasionally very important to include both options within your regex. Here's how you would do that:
https?
The ?
after the "s
" means that there can be 0 or 1 "s
" - meaning http and https are both included. The question mark only affects the ONE character in behind it, unless you put them inside parentheses, as you'll see in just a moment. Put this together with the escaped-slash we learned earlier, and you get:
https?:\/\/
The \/\/
pattern is the two // with escape slashes. A single slash would look like \/
, and an optional slash would look like \/?
We don't need to escape the : because it's not a special character.
To include the "www." you would include the following part:
(www\.)?
The parentheses around the "www.
" mean (everything in here)
- just like in Algebra class. We still have to escape the .
and we add the ?
at the end to show that it's not required, that there can be 0 or 1 "www.
"
Putting the Pieces Together
If the site we are using is http://www.website.com, our whole regex targeting it would look like:
https?:\/\/(www\.)?website\.com\/?
This will match:
http://website.com
https://website.com
http://www.website.com
https://www.website.com
http://website.com/
https://website.com/
http://www.website.com/
https://www.website.com/
Note that these URLs include all the combinations of http, https, www or no www, and the final /
The Beginning and The End
In this older interface, we also need to tell the regex where to stop and end. This is done by using two other special characters, ^
and $
. The ^
marks the beginning of the regex, and the $
marks the end. Using the example from above, this would be your regex:
https?:\/\/(www\.)?website\.com\/?
To use this example, you would add the following to your regex field:
|
Back to the beginning (Introduction)
Comments
0 comments
Article is closed for comments.