<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kevin Lin on Medium]]></title>
        <description><![CDATA[Stories by Kevin Lin on Medium]]></description>
        <link>https://medium.com/@kevinlin_71519?source=rss-38e46e9b6157------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ikE5_FbJYAxKKjBK</url>
            <title>Stories by Kevin Lin on Medium</title>
            <link>https://medium.com/@kevinlin_71519?source=rss-38e46e9b6157------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 22:00:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kevinlin_71519/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Junior Engineer’s Review of Triplebyte]]></title>
            <link>https://medium.com/@kevinlin_71519/a-junior-engineers-review-of-triplebyte-4b922dc37f3e?source=rss-38e46e9b6157------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b922dc37f3e</guid>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[Kevin Lin]]></dc:creator>
            <pubDate>Fri, 15 Nov 2019 01:24:30 GMT</pubDate>
            <atom:updated>2019-11-15T01:25:48.239Z</atom:updated>
            <content:encoded><![CDATA[<p>Triplebyte is a popular job matching system that is somewhat like Hired with one unique caveat: Triplebyte doesn’t ask for any background information before assessing you as a candidate. I think this is a fantastic approach to technical interviews as it allows you to be interviewed by ability as opposed to experience.</p><p>I thought I’d try it out since I’m very much a junior engineer and getting that first job is very difficult when most employers just look at my resume, see that I don’t have three years of experience, then trash my application before I even get to show any value as an engineer.</p><h3><strong>Intro</strong></h3><p>I’m a graduate of Stony Brook University as a Biomedical Engineer. In college I’ve had some coding experience but not a tremendous amount. I was, however, exploring software in my junior year and learned a tremendous amount through self-studying on online resources. After college I went to Fullstack Academy on a scholarship and learned the NERDS stack in detail. After Fullstack I worked at a startup as the lead engineer and was in charge of building out a large part of the product. This was mainly freelance work and it was less than a year of experience, but I did learn a tremendous amount as I was constantly in talks with senior engineers who would teach me things that I wasn’t so familiar with like DevOps.</p><h3><strong>The Process</strong></h3><p>I applied to Triplebyte and set my preference as a Frontend Engineer. I feel the most comfortable doing frontend engineering as I’ve dedicated a lot of time learning it and building projects on the frontend.</p><p><strong>The Quiz</strong></p><p>The Triplebyte process starts with a quiz. The quiz covers a wide range of things like web security, css, javascript, C#, etc. I thought it was pretty fair but there were definitely some problems about concepts that I’ve never heard of. Immediately after the quiz you get a breakdown of how well you did on the quiz. I passed the quiz and received an invitation to the final technical screen.</p><p><strong>The Technical Interview</strong></p><p>Triplebyte is very transparent on what they ask on the technical interview. I won’t cover the exact problems they asked me because I respect their platform, but I’ll give a gist of what they asked. The technical interview is broken down into three parts for frontend engineers: 1. A portion where you build out a web application, 2. A short response section where you’re asked a variety of problems: web related problems, academic cs problems, and data-structures and algorithms, 3. a system design problem where you have to articulate how you’d build out a system giving a prompt</p><p><strong>Part 1: Coding</strong></p><p>In this section, the interviewer gave me a picture of a web application layout and basically tiers of functionality I needed to complete. There were five tiers in total and he informed me that I did not have to finish all the tiers and doing so wasn’t super common. This web application had to be built on your location machine, the interviewer watches you build it out through screen share on Google hangouts. This portion was relatively easy for me as I was very confident in building. I setup a create react application as recommended and started to build out the app and style it. The application itself did have one piece of functionality that was challenging and I had to come up with a clever solution to engineer it out. This was tier 4/5 and I approached this particular problem with a 2D array, which I had to copy at some point. I notified the interviewer that I wanted to copy the 2D array and that I knew using the JSON.stringify method wasn’t the best way to do it, but he just told me to go ahead and use it anyways. I was later docked for doing this, which I’ll talk about later on. Anyways, I managed to complete all five tiers in the given time and basically built out the entire app without some of the styling. He seemed pretty happy with my solution.</p><p><strong>Part 2: Short Response</strong></p><p>After taking a five minute break, we resumed and my interviewer started to ask me short response problems. I was asked a wide range of questions from web development, both backend and frontend, to academic CS stuff. He also asked me about really particular ways to attack a web application that I’ve never heard of and about new web standards that I wasn’t strong about. Aside from React specific problems, CSS, and some datastructure problems, I did not feel like I did well on this portion. I did approach a lot of the problems as best I could, but I was unsure if I answered them properly. I told my interviewer beforehand that I wasn’t sure but I’ll take a jab at them. He also asked me algorithms at this portion,both of which I was very confident about solving. However, I had to vocalize the solution as opposed to whiteboard it out or code it out. This was significantly harder to do because I did not know if my interviewer understood exactly what I meant and because there were edge cases I could not show. My interviewer never instructed me to whiteboard or code on my local environment for this part which made this part significantly more difficult than it had to be. In hindsight, I should have told him that I wanted to whiteboard it out because I do feel confident doing algorithms but it is very hard for me to just vocalize a solution without being able to show what I mean.</p><p><strong>Part 3: System Design</strong></p><p>I was asked a pretty standard system design problem here. Honestly I am not really prepped in system design so this portion was not a great portion for me. The question was pretty board and I definitely could have approached it better. I did not ask my interviewer many clarifying questions and did not try to get him involved in the solution, instead I gave my solution and completely forgot to mention about how I’d scale my system but I gave him a fair answer that made sense.</p><p><strong>The Results</strong></p><p>Unfortunately I did not manage to pass Triplebyte’s interview process, but they did send me a very useful email with resources on things I can focus on. Looking over the email, It seems that I did not really do that poorly on the interview. In fact they said my coding ability was very good and that I did okay on the system design part other than not talking about scaling it. They did mention that I struggled on the short response portion which makes perfect sense to me. It seems overall I passed two sections and did okay on one; I was able to answer most of the short response questions but had trouble on really specific topics. Despite doing an overall okay job however, I did not pass.</p><p><strong>My Overall Thoughts</strong></p><p>I was pretty bummed out about not passing, but I have no bad feelings against Triplebyte. If anything my experience gives more legitimacy to Triplebyte’s screening process. I did decently well and still was not able to pass. Here are my overall thoughts on Triplebyte in a list:</p><p><strong>Criticisms</strong></p><ul><li>I’m not sure how Triplebyte scales each part of the process but I definitely believe that part two should not have as heavy of a weight as part one. Being able to code and do the job is more important than knowing specific niche details that a candidate could literally read up on at any time. I also was surprised that I was not asked any OOP or FP problems, instead I was asked security problems and about a data structure I’ve never heard about as a frontend engineer which doesn’t directly tie into what I would be doing on the job.</li><li>For the algorithm section, I really wish the interviewer told me to open my code editor so I could better convey my thought process to him. Verbally explaining an algorithm solution is strange because it’s hard to cover every possible edge case without visualization of some sort. I also had no idea if he understood what I was saying because the only feedback he gave me was “yeah.”</li><li>My email said I shouldn’t use the JSON.parse()/stringify way to copy a 2D array, but I knew that it wasn’t best practice and asked my interviewer beforehand if I should use it. I know that I can use spread or flatten in a loop. Why was I docked for something I was told was okay to do?</li></ul><p><strong>Things I Liked</strong></p><ul><li>I’m glad the process isn’t easy. It definitely gives value to people actually passing the assessment. It definitely feels like you need to perform really well on every section to pass.</li><li>The feedback was fantastic and I was very surprised that they composed a detailed review in such a short period of time.</li><li>The idea is fantastic. I like how there is no bias beforehand and that the same questions are asked regardless of your background as a result. I definitely feel more confident now despite failing the process because I did a decent job answering the same questions a mid-level or senior engineer would be asked.</li></ul><p><strong>Closing</strong></p><p>I overall enjoyed the experience but it wasn’t perfect by any measure. I think Triplebyte’s process is the right direction for job recruiting sites because there is a guarantee that someone who passed the assessment is talented enough for a job in engineering. I was upset that I didn’t make it because I couldn’t answer a few short response questions. I will most definitely try the process again in four months after reviewing the feedback they gave me. I felt like I was really close, but I don’t know how true that thought is.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b922dc37f3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Quick Introduction to Typescript]]></title>
            <link>https://medium.com/@kevinlin_71519/a-quick-introduction-to-typescript-1e8df33b25ea?source=rss-38e46e9b6157------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e8df33b25ea</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Kevin Lin]]></dc:creator>
            <pubDate>Thu, 12 Sep 2019 17:39:42 GMT</pubDate>
            <atom:updated>2019-09-13T19:09:19.576Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7QwRdAxb9Q8wejjQJiFJsQ.png" /></figure><p>TypeScript is a very popular and rising technology in web development. In this article I’ll cover what TypeScript is, why it’s useful, and how to get started and write your own TypeScript code.</p><h3><strong>What is TypeScript?</strong></h3><p>TypeScript is a wrapper that wraps over JavaScript and provides new features that we can use, most notably types. TypeScript cannot run in the browser and has to be compiled into JavaScript before the page is run. We’ll cover how to write your own TypeScript code then convert that into JavaScript later on.</p><h3>Why TypeScript?</h3><h4><strong>Static vs Dynamic</strong></h4><p>To understand the advantages of TypeScript, we have to first define the difference between statically typed and dynamically typed languages. A statically typed language is a language in which the user must specifically define the types of each variable. This is advantageous because this allows the compiler to do type checking which allows catching of very trivial bugs at an early stage before any code is even run. Dynamically typed languages like JavaScript and Python interpret the types of the variables on the script, essentially guessing for the user. JavaScript in particular will do type coercion to compile the code. The main tradeoff is that dynamically typed languages are less verbose, but will not catch bugs in early stages which could potentially cause lost time in debugging because users will have to find the bug manually.</p><p>Here’s a few examples from TypeScript’s official playground that demonstrate some disadvantages of regular JavaScript:</p><p><strong><em>Example 1: Type Coercion</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eM9i7EJ3ifRNKaesDqh98w.png" /></figure><p>What happens in the code above? We passed an object into out Greeter constructor function, setting the greeting property of our Greeter instance to an object rather than a string. When we call greeter.greet(), instead of throwing an error, JavaScript will coerce our object into a string and return:</p><pre>Hello, [Object object]</pre><p><strong><em>Example 2: Non-matching type comparisons</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HnPtU_RHmrXmb4dZD8S45w.png" /></figure><p>In the code above, we create a button, give it an id of 1, then add an event listener to our document body, which will alert us if we click something with that button’s id. However, if we actually try clicking the button nothing will happen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*9z2lm48961JnEvmsc0tJUg.gif" /></figure><p>Why doesn’t our button work? If we do further analysis and log our id, we’ll see that it is actually a string, not a number:</p><pre>console.log(typeof evt.target.id) =&gt; &quot;string&quot;</pre><p>Attribute tags become strings even if we define them as numbers. This would have been a difficult bug to detect without knowing that the id property is always a string as JavaScript doesn’t throw any errors. A statically typed language would not let us compare strings to number types and would have caught this bug right away!</p><p>Now let’s look at the same examples typed in TypeScript:</p><p><strong><em>Example 1: Type Coercion</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fgqi6U9cY_Djih64f9720g.png" /></figure><p>The code we’ve run before now gives us an error before even running. If we hover over the problematic lines we see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yX9oXMmUMh8fgsGjsWTBSQ.png" /></figure><p>We’re told right away that we’re trying to input an object when a string is expected!</p><p>Note: Here we defined a type in the input as string, this is necessary for TypeScript to detect that we didn’t get the right input;</p><p><strong><em>Example 2: Non-matching type comparisons</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dg3GW2n42Eynn8ljazo6ow.png" /></figure><p>Notice how in the above code we now have two error indicators, if we hover over them we see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yJ99sYeqK1isQswRe0VxHA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QqVRVJ0EZc5j5psh98uKAQ.png" /></figure><p>We’re told even before running our code that it will not work! Notice that this code is still vanilla JavaScript and isn’t typed in TypeScript format yet.<strong> Writing statically is optional in TypeScript!</strong></p><h4><strong>Ability to use modern Javascript features</strong></h4><p>Another advantage of TypeScript is that it can be compiled down to an older version of JavaScript. This is advantageous because it allows us to use newer features of JavaScript without worrying about browser compatibility.</p><h3><strong>How to write your own TypeScript</strong></h3><p>To install TypeScript, make sure you have Node Package Manager installed and run the following command:</p><pre>npm install typescript -g</pre><p>To check your installation, you can run the following command:</p><pre>tsc -v</pre><p>Next, instead of creating a file with the extension .js, we create one with the extension .ts. This represents a TypeScript file:</p><pre>touch index.ts</pre><p>Inside index.ts, we can start writing some TypeScript code. TypeScript can be written as regular JavaScript or with types. The TypeScript compiler, which we’ll see later on, will tell us as we’re trying to compile to JavaScript if it detects any errors regardless of whether we type vanilla JS or TypeScript within our TS file. To take full advantage of TypeScript, we should write out code in TypeScript. Here’s how we can define types in TypeScript:</p><p><strong>Variable Declarations:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sF7xp9eiIOhhJBOpvPs0Bw.png" /></figure><p><strong>Methods:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uccdRD48IUEFKeuPX32rhA.png" /></figure><p>Note, if you do not return the type you declared, the TypeScript compiler will throw an error</p><p><strong>Here’s some common types:</strong></p><p>Numbers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Pa-LRoxMTD7Vt1Vy8WXAfw.png" /></figure><p>Note: integers, doubles, floats, etc are all just considered number in TypeScript. We can redeclare this variable as any numeric type.</p><p>Strings:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zFkS9QYhbGeR_HfIfhiejg.png" /></figure><p>Note: we can’t define individual characters in TypeScript or JavaScript, they are just considered strings.</p><p>Any:</p><p>Any is the default type in TypeScript and reflects the behavior of regular JavaScript variables in which they can be assigned to any types, even if they change:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XVSq_WEDJmfPXt156rY4zw.png" /></figure><p>Arrays:</p><p>Arrays are declared with the type followed by square brackets defining the depth of the array</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*44NTNFxNO1OgDcrbIuNGEA.png" /></figure><p>Tuples:</p><p>Tuples are arrays with defined types and sizes, the following tuple can only be declared as an array of length 2 where the 0th element is a number and the 1th element is a string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OnoYjh4ZeYScJXWDWrvsEg.png" /></figure><p>Enums:</p><p>Enums are special user declared variables that are mapped to numbers, heres an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KbAR040ykiKLJG_0k01J2g.png" /></figure><p>If we log each color, we’ll get a number:</p><pre>console.log(Color.Red) =&gt; 0<br>console.log(Color.Blue) =&gt; 1<br>console.log(Color.Black) =&gt; 2</pre><p>We can change these numbers by manually defining them, but doing so will change the values of all corresponding numbers which increment by one corresponding to the previous value:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tXF4GQzL31tPAoiBSwiyxw.png" /></figure><pre>console.log(Color.Red) =&gt; 0<br>console.log(Color.Blue) =&gt; 100<br>console.log(Color.Black) =&gt; 101 // Blue&#39;s numeric value + 1</pre><h4><strong>Connecting a TypeScript file to an HTML file</strong></h4><p>After writing the necessary code, we can convert our TypeScript file to JavaScript by using the terminal, navigating to the directory of our project folder and running:</p><pre>tsc filename.ts</pre><p>When we run tsc filename.ts, the TypeScript compiler first looks for any issues in the code. If there are none, TypeScript then compiles our code down to a specified version of JavaScript, such as ES2015. This is created as a new Javascript file called filename.js.</p><p>If we want to convert multiple TypeScript files in one go, start by initializing a TypeScript config in your project directory by running:</p><pre>tsc --init</pre><p>This will create a tsconfig.json file which will allow you to change your TypeScript configurations and to use the command tsc to convert all the TypeScript files in the directory to JavaScript files.</p><p>Now, since we have a JavaScript file, we can simply link it to our HTML file normally; linking it through a script tag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ti61XCwoe-Rm7gcJxiSlw.png" /></figure><p>We don’t use the file extension .ts here because TypeScript files cannot run in the browser.</p><h3>Advanced Features</h3><p>Due to the nature of this being an introductory article, I’ll only briefly cover what else is possible using TypeScript. If you’d like to learn more about any individual feature, please follow the links attached to each explanation.</p><h4>Interfaces</h4><p>An interface is an abstract type that a user creates to define a behavior classes, and inputs in the case of TypeScript, must follow. Think of it as a blueprint for an input. Here’s an example of a simple interface:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gezeMKZznpSxVs4tilzgYw.png" /></figure><p>Notice how when we call bark without all the properties in our interface Dog, we get thrown an error. This is because our input doesn’t follow our blue print, or interface which requires the properties name, furColor, and age. Here’s the error we get if we hover line 55:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m7eYrUe1LXlfYMyFH_-n_Q.png" /></figure><p>So we can use interfaces to restrict the inputs we receive in our functions, but here having the properties furColor and age are unnecessary to pass into our bark function as they aren’t used. We could remove them but what if we wanted to reuse our interface for a function that does use those properties? We could make them optional! This is done by simply putting a “?” right after the property name like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YwMSf9aZ7PQWkE8PNRBkOg.png" /></figure><p>Notice how both of our problems from before are gone.</p><p>Another way that interfaces can be used and are commonly used in languages like Java are to enforce rules onto a class like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zfBXobjteTjhVgKvUV97nA.png" /></figure><p>By simply adding the keyword implements, we can force a class to follow the rules of an interface. I intentionally did not fill the class body for the purpose of demonstration. If we hover the error in the code snippet above, we see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NcJZi0SxLxTuYOD3kDiCKg.png" /></figure><p>We’re getting thrown this error because our class Dog implements our interface DogInterface but doesn’t follow its restrictions. According to our DogInterface, the class Dog must have a name property!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I2jSoDR5KSnt70lRsot_qg.png" /></figure><p>After adding the name property, we no longer receive an error because now our class follows our interface’s rules.</p><p>We can also add methods to interfaces like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzonlIx6UgURrFmylKWEyw.png" /></figure><p>Our interface now requires us to have a bark method that returns a type string.</p><p>We can also make certain properties readonly; they can only be instantiated then never changed afterwards:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mo7rP_aPNZ9PTxlwd7_zLQ.png" /></figure><p>Notice if we change our readonly property, we get an error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1Cm9AOHo_5kVHKNl2dFLdQ.png" /></figure><p>If you’d like to learn more about interfaces, check out these two links: <a href="https://www.typescriptlang.org/docs/handbook/interfaces.html">https://www.typescriptlang.org/docs/handbook/interfaces.html</a> <a href="https://en.wikipedia.org/wiki/Interface_(Java)">https://en.wikipedia.org/wiki/Interface_(Java)</a></p><h3>Closing</h3><p>A good way to learn TypeScript is by converting a project you’ve written in JavaScript to TypeScript. If you don’t know where to start, can you convert the following Tic Tac Toe game logic I’ve written to TypeScript?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72a8b422a3f21f83f607414445dcdbaa/href">https://medium.com/media/72a8b422a3f21f83f607414445dcdbaa/href</a></iframe><p>Here’s the solution:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc5d1f80416f1a7b12a197daa83559af/href">https://medium.com/media/dc5d1f80416f1a7b12a197daa83559af/href</a></iframe><h3>Great TypeScript Resources</h3><p><a href="https://www.typescriptlang.org/index.html">https://www.typescriptlang.org/index.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e8df33b25ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An Introduction to Regular Expressions With an Example]]></title>
            <link>https://medium.com/@kevinlin_71519/an-introduction-to-regular-expressions-with-an-example-4791e21ee8e8?source=rss-38e46e9b6157------2</link>
            <guid isPermaLink="false">https://medium.com/p/4791e21ee8e8</guid>
            <category><![CDATA[regex]]></category>
            <dc:creator><![CDATA[Kevin Lin]]></dc:creator>
            <pubDate>Mon, 19 Aug 2019 01:46:45 GMT</pubDate>
            <atom:updated>2019-09-12T18:21:58.357Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SNe_ijdMwytxpyvPbp_7hA.jpeg" /></figure><p>Have you ever wanted to search for and change a really specific sequence in a really long string? For example, maybe you want to pull all the phone numbers from a phone book that is correlated to a barbershop to create a list of barbershops and phone numbers. Regular expressions are special text strings that are used to search for a specific pattern within a string. Knowing how to construct these special text strings will save you from typing several lines of code to search strings manually.</p><p>Here’s an example of a regular expression that matches with phone numbers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/0*CEWLtt7njYeXcOsV" /></figure><p>Looks bizarre doesn’t it? We’ll walk through this expression character by character. You can follow along by using this link to Regexr: <a href="https://regexr.com/4jd4i">https://regexr.com/4jd4i</a>. You can edit the expression or the text as you please.</p><p>We’ll start from the outmost elements of the regular expression. Regular expressions are instantiated in between forward slashes like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/0*43IA6VJ1SIJkkQ1z" /></figure><p>The query we want to search for is between our two forward slashes. The g following our closing forward slash is called a flag. There are five flags in regular expressions, we’ll cover two of the most useful flags in this short introduction to regular expressions:</p><p><strong>g</strong> — The g flag stands for global. If the g flag is tacked on the regular expression will match with all cases within the string that matches the given regular expression. In our example, if we remove our g flag, we will only match the first phone number like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/906/0*JdDImBjeZINSSice" /></figure><p>By removing the g flag, our regular expression now only matches the first instance that matches our regular expression, the first phone number in the string.</p><p><strong>i</strong> — The i flag stands for insensitive, this means that your regular expression is case-insensitive and will ignore casing in its search. If you construct a regular expression with a lowercase a but have the i flag tagged on, the regular expression will also match with an uppercase A.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NjhQCA24MG86zRl_" /></figure><p>On the left, without the i flag, we only match letters that are lowercase. In contrast, on the right, with the i flag, we match all letters despite casing. Also notice that we can combine flags by simply placing them next to each other. In our second example, the rules of both the i flag and the g flag are applied.</p><p>Moving on to within our regular expression, we see a backslash “\”. Backslashes represent escape characters and are usually followed by a special character. Just like in languages like JavaScript, Regex has a set of reserved characters that can only be used if a backslash is placed before them. For example, the character “/” is reserved in regex because it can represent the beginning or end of a regular expression, if we want to specifically search for it, we’d include a “\” before it.</p><p>Next, we see an open parens. Since we placed a backslash before the parens, we can assume that the open parens is a special character, we’ll cover those later on. For now just know that the open parens is a special reserved character and the combination “\(“ just allows us to match an open parens, “(“.</p><p>Next, we see a question mark. In regex, a question mark is a special character that means that the expression right before it is optional. In our case, since the question mark comes after “\(“, we’re basically just saying that an open parens is optional. This is a nifty tool if we want to select similar strings that have some differences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/0*dOrzCnQpb9OP0NNK" /></figure><p>Since the h is followed by a question mark, it is optional. We will either match “hi” or “i”.</p><p>Next we see “[0–9]”. Square brackets in regex is like an or statement, the regex will match with anything within the square brackets. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/0*CE0ApC0Wvs1-hm-V" /></figure><p>In the expression above, the regular expression is looking for a lowercase i followed by either an ‘h’ or an ’n’. Since we have a g flag, we match both “ih” and “in”.</p><p>We can also use a “-” to include all characters between two characters on the ASCII table. [0–9] means select any number from 0 to 9, while [a-z] means select any letter a through z.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/0*l2l5L-0bXjA2fpPG" /></figure><p>The above expression says match with either a or b or a number between 0 to 9 inclusive. The g flag allows us to select everything that follows this rule. The small broken rectangle highlights indicate that the regex is actually matching one item at a time as opposed to a whole uniform phrase; we are matching “0” instead of “0234.” It is important to note that square brackets only match with one character that matches with something within the brackets.</p><p>Next we see curly braces and a number embedded within them “{3}”. Curly braces are special phrases in regex that allow us to specify how much of the previous item we want to match. “[0–9]{3}” means match three back to back numbers 0 to 9 inclusive. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/0*haXTtdYnG6jHO4uA" /></figure><p>The phrase above means select three consecutive numbers 0 to 5 inclusive. Notice how we have a uniform rectangle as opposed to single rectangles like in the previous example. We are matching with “234” in this example as opposed to individual characters because of the {3} selector.</p><p>Now we know all the rules to understand the phone number regular expression, from beginning to end:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/0*wZYyJLzlNlLM3uQ6" /></figure><p><strong>First third (\(?[0–9]{3}\)?)</strong></p><p>“\(?” means match with an optional open “(“. [0–9]{3} means match with three consecutive numbers 0 through 9 inclusive and “\)?” means match with an optional closing “)”. Our first third “\(?[0–9]{3}\)?”, thus will match with “(123)”, “123”, “(123”, or “123)” where 123 can be any three consecutive numbers.</p><p><strong>Second third ([-\s]?[0–9]{3})?)</strong></p><p>“[-\s]?” means match with an optional “-” or an optional whitespace. As we stated before, a backslash indicates that a special character will follow. The combination “\s” actually means whitespace (a list of all these special characters will be included at the bottom in the resources section). “[0–9]{3}” just means, match with any three consecutive numbers. So “[-\s]?[0–9]{3}” will match with “-123”, “123”, or “ 123” where 123 can be any three consecutive numbers.</p><p>Putting our first and second third together, we will currently match with:</p><p>“(123)123”, “(123) 123”, “(123)-123”. “123123”, “123 123”, “123–123”, “(123123”, “(123 123”,”(123–123&quot;,”123)123&quot;, “123) 123”, or “123)-123”.</p><p><strong>Final third ([-\s]?[0–9]{4})</strong></p><p>Similar to our second third, this matches an optional whitespace or dash followed by four consecutive numbers.</p><h3>A few other important selectors</h3><p>Parens “()” — parens let us group elements which is really useful if we want to apply special characters such as a question mark on a word:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/0*uovpoKXhn_uEsL2x" /></figure><p>Or “|” — really useful tool if you want two different things with one regular expression. The exact same as an or statement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/0*Jw8JS4jbhphgznJk" /></figure><p>Starts with “^” — what if we wanted to only select words that start with a specific letter as opposed to selecting something in between a word? We can do that by tacking a carrot before what we want to match to start with:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*V6EeN4UKoazJqLfJ" /></figure><p><strong>The not selector “[^query]”</strong> — if you want to match everything except a specific query, you can create square brackets followed by a carrot followed by a query like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rpiWbevhXyJWjwjh" /></figure><p>The expression above matches everything other than capital letters.</p><h3>Some Special Characters in Regex</h3><p><strong>\w</strong> — matches any word characters which include letters, numbers, and an underscore regardless of casing</p><p><strong>\d</strong> — matches any digit.</p><p><strong>\s </strong>— matches white spaces.</p><p><strong>\W</strong> — matches anything that isn’t a word character, opposite of \w.</p><p><strong>\D</strong> — matches anything that isn’t a digit, opposite of \d.</p><p><strong>\S</strong> — matches anything that isn’t a whitespace, opposite of \s</p><p><strong>. </strong>— matches with anything, wildcard character.</p><p><strong>*</strong> — matches zero+ of the expression before it. “ /(hi)*/”, matches “”, “hi”, “hihi”, “hihihi” and so on.</p><h3>Usage examples in JavaScript</h3><p>Replacing words/characters in a string</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CdJD61xphKJdhKwk" /></figure><p>Finding things in a string and storing them in an array</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5oZTG6nugsWGVUJr" /></figure><p>Selecting the first letter of each word in a string</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L5A1-PLGN5UT3T6o" /></figure><p>\b represents word boundaries, marks the start of a “whole” word. Read more about them <a href="https://www.regular-expressions.info/wordboundaries.html">here</a>.</p><h3><strong>Disadvantages</strong></h3><p>Regex is very useful and could be used to simplify many string-based algorithms, but doesn’t come without disadvantages. For one, regex is unpredictable performance wise. Here’s a great article by Liz Bennet that covers the performance of regular expressions: <a href="https://www.loggly.com/blog/regexes-the-bad-better-best/">https://www.loggly.com/blog/regexes-the-bad-better-best/</a>. To summarize, using special matching queries usually seen in shorter regular expressions is not as efficient as a longer, more specific regular expression. We also have to consider the particular language-specific methods we use with the regular expression as well as the different kinds of flags we include. It is also worth noting that a non-matching regular expression will usually be less performant than one that always matches.</p><h3>Closing</h3><p>Regular expressions are great and can save a ton of time. They do seem hard to read, but, just like programming, become easy to read and write over time. I’ve only covered regular expressions briefly in this article. If you’d like a more comprehensive guide to regular expressions, check out <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">MDN’s Regular Expression Introduction</a>. Thank you for reading!</p><h3>Challenge</h3><p>Notice how the regular expression I introduced matches with strange edge case phone numbers such as “(1231231234”, “123)1231234”, etc. Can you modify that regular expression to exclude those incorrectly formatted numbers?</p><h3>Fantastic Resources for Learning Regular Expressions:</h3><p><a href="https://regexone.com/">https://regexone.com/</a></p><p><a href="https://regexr.com/4jd4i">https://regexr.com/</a></p><p><a href="https://www.regular-expressions.info/wordboundaries.html">https://www.regular-expressions.info/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4791e21ee8e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>