<?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: Jalaj</title>
    <description>The latest articles on DEV Community by Jalaj (@jalaj).</description>
    <link>https://dev.to/jalaj</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%2F61575%2F5fefbc5b-08df-4daa-9484-12fc3de7a021.jpg</url>
      <title>DEV Community: Jalaj</title>
      <link>https://dev.to/jalaj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jalaj"/>
    <language>en</language>
    <item>
      <title>Managing Two GitHub Accounts on Your Mac</title>
      <dc:creator>Jalaj</dc:creator>
      <pubDate>Wed, 01 Nov 2023 11:15:08 +0000</pubDate>
      <link>https://dev.to/jalaj/managing-two-github-accounts-on-your-mac-46dl</link>
      <guid>https://dev.to/jalaj/managing-two-github-accounts-on-your-mac-46dl</guid>
      <description>&lt;p&gt;If you find yourself juggling between work and personal GitHub accounts on your Mac, you're not alone. This guide will walk you through the steps to set up and manage two separate GitHub accounts with ease. I've tested these steps on MacOS Ventura M1, but they should work for most Mac users. If you're using Linux, you may need to adapt the commands accordingly. Apologies to Windows users as this guide is tailored for macOS.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Quick Tip&lt;/strong&gt;: For a summary of the steps, scroll to the bottom for a TL;DR.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Setting up SSH Keys
&lt;/h2&gt;

&lt;p&gt;We need to set up two SSH keys, one for work and another one for the personal account. Give them clear names, like &lt;code&gt;jalaj-work-github&lt;/code&gt; and &lt;code&gt;jalaj-personal-github&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's how you can create SSH keys for both accounts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen -t rsa -C "jalaj@work.com" -f "jalaj-work-github"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running this command, you'll be prompted to enter a passphrase. You can choose something memorable or leave it empty to avoid entering it every time you push or pull.&lt;/p&gt;

&lt;p&gt;Similarly, create an SSH key for your personal account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-keygen -t rsa -C "jalaj@personal.com" -f "jalaj-personal-github"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running these commands, navigate to the SSH directory by using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ~/.ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the ls command to verify that you have four files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jalaj-work-github
jalaj-work-github.pub
jalaj-personal-github
jalaj-personal-github.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One is a public file, and the other is private. Now, copy the content of the .pub file and add it to the SSH keys section of the respective GitHub account.&lt;/p&gt;

&lt;p&gt;Here's how to copy the content to your clipboard:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pbcopy &amp;lt; jalaj-work-github.pub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, go to GitHub and set up the SSH key. You can do that by following the steps mentioned in the &lt;a href="https://docs.github.com/en/github-ae@latest/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account#adding-a-new-ssh-key-to-your-account"&gt;github docs to add SSH key to your account&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After completing this step, add the private keys to the Apple Keychain. Assuming you are still in the ~/.ssh directory, use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh-add --apple-use-keychain jalaj-work-github
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace "jalaj-work-github" with the actual file path.&lt;/p&gt;

&lt;p&gt;Do the same for the other account.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring SSH
&lt;/h2&gt;

&lt;p&gt;Now, create a configuration file for SSH to know which configuration to use based on the origin.&lt;/p&gt;

&lt;p&gt;In the ~/.ssh directory, create a file named config if it doesn't already exist:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch config &amp;amp;&amp;amp; code config # or vi config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following content to the config file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/jalaj-work-github

Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/jalaj-personal-github
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding this, whenever you pull a repo or create a new repo, you need to update the remote URL for the repo to match the host value in the config.&lt;/p&gt;

&lt;p&gt;Here's a one-liner to do just that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote set-url origin git@github.com-personal:username/reponame.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Breaking it down:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;"&lt;a href="mailto:git@github.com-personal"&gt;git@github.com-personal&lt;/a&gt;" is the host added in the config file.&lt;/li&gt;
&lt;li&gt;"username" is your GitHub username.&lt;/li&gt;
&lt;li&gt;"reponame" is your GitHub repository name.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up User Information
&lt;/h2&gt;

&lt;p&gt;To let Git know your email and username to attach to the commits, you have two options: manual setup for each repo or using Git config files.&lt;/p&gt;

&lt;p&gt;For the manual setup for each repo, run these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --local user.email "jalaj@personal.com"
git config --local user.name "jalaj"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need to remember to do this for every repo on your system.&lt;/p&gt;

&lt;p&gt;To make this easier, you can set a global config for work/personal GitHub accounts and manually configure the other one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global user.email "jalaj@work.com"
git config --global user.name "jalaj"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For an even easier approach, you can use Git config files.&lt;/p&gt;

&lt;p&gt;First, open or create a &lt;code&gt;~/.gitconfig&lt;/code&gt; file in the root directory, and create two other files: &lt;code&gt;~/.gitconfig-work&lt;/code&gt; and &lt;code&gt;~/.gitconfig-personal&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch ~/.gitconfig ~/.gitconfig-work ~/.gitconfig-personal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open &lt;code&gt;~/.gitconfig-work&lt;/code&gt; in your favorite editor and add the following values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[user]
 name = jalaj
 email = jalaj@work.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do the same for the &lt;code&gt;~/.gitconfig-personal&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Now, open &lt;code&gt;~/.gitconfig&lt;/code&gt; and add the following condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[includeIf "gitdir:~/projects/personal/"]
  path = ~/.gitconfig-personal
[includeIf "gitdir:~/projects/work/"]
  path = ~/.gitconfig-work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration loads the correct config file based on the directory where your project resides. Feel free to change the directory to match your work and personal projects.&lt;/p&gt;

&lt;p&gt;With all these steps, you can now use both personal and work GitHub accounts on a single system, and everything will work automatically due to the one-time setup you've completed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;h3&gt;
  
  
  SSH Key Setup
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Generate two SSH keys: one for work and one for personal accounts.

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ssh-keygen -t rsa -C "jalaj@work.com" -f "jalaj-work-github"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ssh-keygen -t rsa -C "jalaj@personal.com" -f "jalaj-personal-github"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Copy the content of the .pub files to your GitHub accounts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Keychain Configuration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Add the private keys to Apple Keychain.

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;ssh-add --apple-use-keychain jalaj-work-github&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Repeat for the other account.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  SSH Configuration
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a &lt;code&gt;config&lt;/code&gt; file in &lt;code&gt;~/.ssh&lt;/code&gt; with host-specific settings.

&lt;ul&gt;
&lt;li&gt;Define Hosts for both work and personal GitHub accounts.&lt;/li&gt;
&lt;li&gt;Specify the &lt;code&gt;IdentityFile&lt;/code&gt; for each.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Update Git Remotes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For existing repositories, update the remote URL to match the host value in the config file.

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git remote set-url origin git@github.com-personal:username/reponame.git&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  User Information
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Set your email and username for commits.

&lt;ul&gt;
&lt;li&gt;For each repository, use:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git config --local user.email "jalaj@personal.com"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git config --local user.name "jalaj"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Or set a global config:&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git config --global user.email "jalaj@work.com"&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git config --global user.name "jalaj"&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Advanced Git Config (Optional)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create and configure &lt;code&gt;~/.gitconfig&lt;/code&gt;, &lt;code&gt;~/.gitconfig-work&lt;/code&gt;, and &lt;code&gt;~/.gitconfig-personal&lt;/code&gt; files to manage user information for different projects based on their directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With these steps, you can effectively manage both your personal and work GitHub accounts on your Mac. If you have any questions or doubts, feel free to reach out, and you can also contact me on Twitter. Thanks for reading!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Everything About JavaScript String.prototype.replace() Method</title>
      <dc:creator>Jalaj</dc:creator>
      <pubDate>Thu, 16 Apr 2020 08:15:22 +0000</pubDate>
      <link>https://dev.to/jalaj/everything-about-javascript-string-prototype-replace-method-8bo</link>
      <guid>https://dev.to/jalaj/everything-about-javascript-string-prototype-replace-method-8bo</guid>
      <description>&lt;p&gt;Replacing text in a string is a very common task and with the help of &lt;code&gt;replace()&lt;/code&gt; method available on &lt;code&gt;String.prototype&lt;/code&gt;, we can do it very easily.&lt;/p&gt;

&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regExp&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nx"&gt;substring&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;newSubstring&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;replace()&lt;/code&gt; method takes two arguments, first argument can be a string or regular expression and the second argument can either be a replacement string or a function whose return value will then be taken as replacement string.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Strings are immutable in JavaScript. That's why &lt;code&gt;replace()&lt;/code&gt; returns a new string&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The simplest use-case of this method is just by providing a string to match as first argument and a replacement string as second argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I Love Java&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Java&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Expected Output: I Love JavaScript&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Replace operations can be easily chained.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I Love Java&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Java&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
              &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Pizza&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// I Love Pizza&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the replace string we can use special replacement patterns, for example we can use &lt;code&gt;$&amp;amp;&lt;/code&gt; to insert the match.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Java is Awesome&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myStr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Java&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;$&amp;amp;Script&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// JavaScript is Awesome&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Some other replacement patterns: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$$&lt;/code&gt; - Inserts a "$".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$`&lt;/code&gt; - Inserts the portion of the string that precedes the matched substring.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$'&lt;/code&gt; - Inserts the portion of the string that follows the matched substring.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is also a pattern &lt;code&gt;$n&lt;/code&gt; which inserts the nth match, but for this to work we have to use regular expression to match the string as explained in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Regular Expressions as first argument in &lt;code&gt;replace()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Matching an exact string is good, but most of the times we find ourselves needing to match a pattern instead. It can easily be done by using a regular expression as the first argument.&lt;/p&gt;

&lt;p&gt;In the following example, the regular expression is defined in &lt;code&gt;replace()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;A Quick Brown Fox&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newstr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/brown/i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;white&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newstr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// A Quick white Fox&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above regular expression we used &lt;code&gt;i&lt;/code&gt; flag. This flag ignores the case.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Checkout &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"&gt;this guide&lt;/a&gt; by MDN to learn more about regular expresssions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's take another example to show the use of &lt;code&gt;$n&lt;/code&gt; replacement pattern.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Regular Expressions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;(\w&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\s(\w&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newstr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;$1 $2 are evil&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newstr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Regular Expressions are evil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/(\w+)\s(\w+)/&lt;/code&gt; - This regex matches two words separated by a space character.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$1&lt;/code&gt; will be equal to whatever first capturing group (regex inside the parenthesis) captures, in our case its the string 'Regular'.&lt;/li&gt;
&lt;li&gt;Similarly &lt;code&gt;$2&lt;/code&gt; will be equal to the result to second capturing group which is 'Expressions'.&lt;/li&gt;
&lt;li&gt;In the end we use &lt;code&gt;$1&lt;/code&gt; , &lt;code&gt;$2&lt;/code&gt; to create new string.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using a Function as a second argument in &lt;code&gt;replace()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;We can use a function as a second argument for &lt;code&gt;replace()&lt;/code&gt;, this function is known as Replacer Function. This function is invoked after the match has been performed. This function's result (return value) will be used as the replacement string.&lt;/p&gt;

&lt;p&gt;The various possible arguments of this function are discussed below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;match&lt;/code&gt; - The matched string.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;p1, p2, ...&lt;/code&gt; - The nth string found by a parenthesized capture group, provided the first argument to replace() was a RegExp object. For example, if &lt;code&gt;/(\w+)(\d+)/&lt;/code&gt;, was given, &lt;code&gt;p1&lt;/code&gt; is the match for &lt;code&gt;\w+&lt;/code&gt;, and &lt;code&gt;p2&lt;/code&gt; for &lt;code&gt;\d+&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;offset&lt;/code&gt; - The offset (index) of the matched string within the whole string being examined. (For example, if the whole string was &lt;code&gt;'abcd'&lt;/code&gt;, and the matched string was &lt;code&gt;'bc'&lt;/code&gt;, then this argument will be 1.)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;originalString&lt;/code&gt; - The full string.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see it in action through an  example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;replacerFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;originalString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;99 Bottles Of Bear On The Wall&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;(\d{2})(\w&lt;/span&gt;&lt;span class="sr"&gt;*&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myStr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;replacerFunction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newStr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 98 Bottles Of Bear On The Wall&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In above example our first capture group matches the digit '99' and second one matches the rest of the string. After that the replacer function return the modified result.&lt;/p&gt;

&lt;p&gt;Let's see how to create a very-very simple and silly url slug generator using &lt;code&gt;replace()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;My Awesome Post&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;urlSlug&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;(\w&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\s(\w&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)\s(\w&lt;/span&gt;&lt;span class="sr"&gt;+&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;p3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;urlSlug&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// My-Awesome-Post&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Aaaand That's all. I hope you learned something useful from this post. If anything is not clear, let me know in comments. :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>regularexpressions</category>
    </item>
  </channel>
</rss>
