Need regex for url pattern -
i need regex validate urls:
bad:
www.*.com www.abc* *.* good:
*abc.com *.com www.abc.com
you use regex like:
^(www\.)?([a-za-z0-9\-]+?)(\.com)$ here's breakdown of how regex works:
^matches begining of string
(www\.)?checks either nothing, or stringwww.literally.
[a-za-z0-9\-]matches letter (capital , lowercase), numbers , hyphen.
([a-za-z0-9\-]+?)matches above 1 or more times, , tries find smallest match.
(\.com)matches string.comliterally.
$matches end of string.
here's live preview of regex in action on regex101.com
this allow urls like:
- www.google.com
- www.apple.com
- jojodmo.com
- stackoverflow.com
- 99-fake-websites.com
but not allow urls like:
- mail.google.com
- data.stackexchange.com
- microsoft.net
- redirect.affiliate.scam.hop.clickbank.gov
if allow subdomains other www (and more 1 of them), allow tlds other .com, above, use:
^(([\w\-]+\.)+?)?([a-za-z0-9\-]+?)(\.[a-za-z]{2,})$ this changes:
(www\.)?changed(([\w\-]+\.)+?)?.
[\w\-]+matches letter (capital or lowercase), numbers, hyphens, , underscores 1 or more times.\.matches character.literally.([\w\-]+\.)+?matches above 1 or more times, finding smallest match.(([\w\-]+\.)+?)?makes above optional
(\.com)changed(\.[a-za-z]{2,})
\.matches character.literally[a-za-z]{2,}matches letter, capital or lowercase, 2 or more times
if add above here's preview of new regex on regex101.com.
Comments
Post a Comment