<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prachi Tripathi</title>
    <description>The latest articles on DEV Community by Prachi Tripathi (@prachi09051999).</description>
    <link>https://dev.to/prachi09051999</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1023149%2F866c4b17-5bfc-428c-a61b-c5d14d3ab3cf.png</url>
      <title>DEV Community: Prachi Tripathi</title>
      <link>https://dev.to/prachi09051999</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prachi09051999"/>
    <language>en</language>
    <item>
      <title>Regex learning by creating URL regex</title>
      <dc:creator>Prachi Tripathi</dc:creator>
      <pubDate>Mon, 27 Mar 2023 11:33:01 +0000</pubDate>
      <link>https://dev.to/prachi09051999/regex-learning-by-creating-url-regex-3g7h</link>
      <guid>https://dev.to/prachi09051999/regex-learning-by-creating-url-regex-3g7h</guid>
      <description>&lt;p&gt;In Software development, &lt;strong&gt;Regular expressions&lt;/strong&gt; (Regex)are handy tools for &lt;strong&gt;pattern matching&lt;/strong&gt; and &lt;strong&gt;text processing&lt;/strong&gt;. &lt;br&gt;
Input field values in a UI form can be validated using &lt;strong&gt;Pattern Matching&lt;/strong&gt;.&lt;br&gt;
This pattern matching can be used to validate input field values in a form shown in UI.&lt;br&gt;
The goal of this article is to teach you how to create a regex for URL validation.&lt;br&gt;
Let's get started! 🚀&lt;/p&gt;

&lt;p&gt;How does a normal URL look like??&lt;br&gt;
&lt;em&gt;&lt;a href="https://www.google.com"&gt;https://www.google.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here's how to add pattern to each part of this url - &lt;/p&gt;

&lt;h2&gt;
  
  
  1. Addition of Prefix(http vs https) -
&lt;/h2&gt;

&lt;p&gt;Websites may use &lt;em&gt;http or https&lt;/em&gt;. Here &lt;strong&gt;'s'&lt;/strong&gt; might or might not  be in the url. The &lt;strong&gt;'?'&lt;/strong&gt; quantifier adds optionality to the preceding character or group. Also for addition of forward slash character(/), you need to use a backslash() since the forward slash by default ends the pattern.&lt;br&gt;
So let's add the pattern to the Regex like this - &lt;strong&gt;((http)s?:\/\/)&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Addition of sub-domain(www) -
&lt;/h2&gt;

&lt;p&gt;The example we took above uses &lt;strong&gt;www&lt;/strong&gt; as a sub-domain name, which is the most common, but let's also look at these URLs -  &lt;a href="https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html"&gt;https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html&lt;/a&gt; and &lt;a href="https://todoist.com"&gt;https://todoist.com&lt;/a&gt;. &lt;br&gt;
www isn't always a subdomain, and it's not always in the URL.&lt;br&gt;
Check out the URL for dev.io. &lt;br&gt;
Regex uses the &lt;strong&gt;/d&lt;/strong&gt; metacharacter to add digits. This part may or may not contain the number.&lt;br&gt;
We use a &lt;strong&gt;Kleene Star(*)&lt;/strong&gt; to represents 0 or more repetition of preceding character or group.&lt;br&gt;
Also for addition of period character(.), you need to use a slash() since by default period character represents all allowed characters. &lt;br&gt;
so let's add the pattern to the Regex for this part - &lt;strong&gt;(www\d*.)?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Addition of domain name -
&lt;/h2&gt;

&lt;p&gt;All alphanumeric(A-Z, a-z, 0-9) and special characters like &lt;strong&gt;_, ., -, ~, /, # and -&lt;/strong&gt; are allowed in the domain name.&lt;br&gt;
To represent alphanumeric characters, \w metacharacter is used in Regex.&lt;br&gt;
&lt;strong&gt;Kleene Plus(+)&lt;/strong&gt; represents 1 or more repetitions of a preceding character or group.&lt;br&gt;
We use &lt;strong&gt;square brackets([])&lt;/strong&gt; inside which we can write all the allowed characters to match the specific characters.&lt;br&gt;
So let's write the Regex pattern for this part - &lt;strong&gt;([\w_.-~\/#-]+)&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Addition of extension(.com) -
&lt;/h2&gt;

&lt;p&gt;Subdomain names can also contain all characters similar to the domain name as well as a question mark.&lt;br&gt;
So let's also write the Regex pattern for this part - &lt;strong&gt;([\w_.-~\/?=#-]+)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now finalising our Regex pattern for the URL - &lt;strong&gt;&lt;em&gt;^(((https:\/\/)|(http:\/\/))(www\d*.)[\w&lt;/em&gt;.-~\/#-]+(.[\w_.-~\/?=#-]+)+)$_&lt;/strong&gt;&lt;br&gt;
Where both the start and end of the line are described by the special metacharacters hat (^) and dollar sign ($).&lt;/p&gt;

&lt;p&gt;That's all for now folks! 🎉&lt;/p&gt;

&lt;p&gt;This might not be the best Regex for all usecases, so let me know what yours is.&lt;/p&gt;

&lt;p&gt;Keep learning and sharing! 🚀&lt;/p&gt;

</description>
      <category>regex</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
