{"id":118,"date":"2011-12-06T11:56:58","date_gmt":"2011-12-06T18:56:58","guid":{"rendered":"http:\/\/www.designersgate.com\/blogs\/?p=118"},"modified":"2011-12-06T11:56:58","modified_gmt":"2011-12-06T18:56:58","slug":"regexp-filter-for-urls","status":"publish","type":"post","link":"https:\/\/designersgate.com\/blog\/regexp-filter-for-urls\/","title":{"rendered":"RegExp Filter For URL&#8217;s"},"content":{"rendered":"<p>I was trying to get a nice short Regular Expression to filter and validate the url&#8217;s entered by an user in an form, and finally I come up with one that you can use as your first pass filter that its been working fine for me.<\/p>\n<p>It&#8217;s not a perfect one, but at least filter most of the commons mistakes user commit while filling out forms, specially from mobile devices:<\/p>\n<pre>[http][s]?[:][\\\/\\\/]([^\\.]\\w[^\\s]*\\.)(.*?\\.)?[a-zA-Z]{2,4}$<\/pre>\n<p>At first looks for the http prefix with:<\/p>\n<p><code>[http]<\/code><\/p>\n<p>Then a conditional search for the https: secure servers prefix:<\/p>\n<p><code>[s]?<\/code><\/p>\n<p>Next the colon:<\/p>\n<p><code>[:]<\/code><\/p>\n<p>Also force the double forward slashes to prevent typing &#8220;http:\/&#8221;:<\/p>\n<p><code>[\\\/][\\\/]<\/code><\/p>\n<p>Next it will look for any group of letters or numbers followed by a dot that never should be preceded by a dot:<\/p>\n<p><code>([^\\.]\\w[^\\s]*\\.)<\/code><\/p>\n<p>This will prevent the user to enter something like:<\/p>\n<p><code>http:\/\/.designersgate.com<\/code><\/p>\n<p>Next will look for optional name followed by dots:<\/p>\n<p><code>(.*?\\.)?<\/code><\/p>\n<p>Finally the expression will look for the last letters after the final dot, with a range from 1 to 4 digits (ex. co, com, mobi):<\/p>\n<p><code>[a-zA-Z]{2,4}$<\/code><\/p>\n<p>Now when you text this regular expression pattern with <a title=\"RegExp Builder OpenSource Software\" href=\"http:\/\/renschler.net\/RegexBuilder\/\" target=\"_blank\" rel=\"noopener noreferrer\">RegexBuilder<\/a> this are the results:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/designersgate.com\/images\/regEx\/reg-urlfilter.jpg\" alt=\"Regular Expression pattern to filter url addresses\" \/><\/p>\n<p>As I said, is not a perfect filter because some mistakes are passed like:<\/p>\n<pre>\nhttp:\/\/ww.com\nhttp:\/\/ww.designersgate.com\n<\/pre>\n<p>But this is intended just in case the user url is something like:<\/p>\n<p><code>http:\/\/en.designersgate.com<\/code><\/p>\n<p>After filtering the first round with this you can also check later on for other errors that you might think are necessary for your application but I think this is a good start.<\/p>\n<p>If you need this filtered for C++ literals here it is:<\/p>\n<p><code>@\"[http][s]?[:][\\\/\\\/]([^\\.]\\w[^\\s]*\\.)(.*?\\.)?[a-zA-Z]{2,4}$\"<\/code><\/p>\n<p>Happy Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to get a nice short Regular Expression to filter and validate the url&#8217;s entered by an user in an form, and finally I come up with one that you can use as your first pass filter that its been working fine for me. It&#8217;s not a perfect one, but at least filter [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[75,76,16,29,83,80,14,81],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-coding","tag-filter","tag-javascript","tag-php","tag-regular-expressions","tag-url","tag-users","tag-validation"],"acf":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":0,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"wp:attachment":[{"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/designersgate.com\/blog\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}