<?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: Oluwajuwon</title>
    <description>The latest articles on DEV Community by Oluwajuwon (@oluwajuwon).</description>
    <link>https://dev.to/oluwajuwon</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%2F252783%2Fdbfd1324-5a03-4eff-b8a0-11d2a34ba32c.png</url>
      <title>DEV Community: Oluwajuwon</title>
      <link>https://dev.to/oluwajuwon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oluwajuwon"/>
    <language>en</language>
    <item>
      <title>Using the string.replace method in Javascript</title>
      <dc:creator>Oluwajuwon</dc:creator>
      <pubDate>Fri, 18 Oct 2019 17:41:52 +0000</pubDate>
      <link>https://dev.to/oluwajuwon/using-the-string-replace-method-in-javascript-1aja</link>
      <guid>https://dev.to/oluwajuwon/using-the-string-replace-method-in-javascript-1aja</guid>
      <description>&lt;p&gt;Earlier today I was working on a &lt;a href="https://www.codewars.com/kata/convert-string-to-camel-case/javascript"&gt;challenge&lt;/a&gt; on Codewars or as they're popularly called; &lt;code&gt;Kata&lt;/code&gt;. While working on it I also did some research on how I could go about solving it, and I stumbled on a string method I had never used before. The replace() method.&lt;/p&gt;

&lt;h1&gt;
  
  
  String.prototype.replace()
&lt;/h1&gt;

&lt;p&gt;According to MDN, the replace method returns a new string after all or part of it has been replaced based on a replacement pattern/string.&lt;br&gt;
The pattern could be a substring or a regular expression (I'm guessing you all know about regular expressions, if you don't you can check this out).&lt;/p&gt;

&lt;p&gt;The replace method takes in two arguments: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The pattern or substring to be matched.&lt;/li&gt;
&lt;li&gt;The new String/Substring or a function that returns a new string, to replace the matched string.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's have a look at the replace syntax&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
const newString = string.replace(substring/pattern, newstring/function)&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Solving the challenge
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IGh2MuyQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/648ppa8mgsimwwbwdv4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IGh2MuyQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/648ppa8mgsimwwbwdv4t.png" alt="The challenge to Convert string to camel case - Codewars"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we want to do is find all the characters in the given string that match either the underscore( _ )  or the hyphen (-) and the alphabets after them,  transform them to uppercase letters and remove the non-alphabetic characters.&lt;/p&gt;

&lt;p&gt;First of all, since the replace method can take a regular expression to be used for matching, we can create an expression that matches what we want to replace.&lt;br&gt;
&lt;code&gt;&lt;br&gt;
const regExp = /[^a-z][a-z]/gi&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's break down the regular expression above&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[^a-z] - this matches any character in the string that is not an alphabet from a-z

[a-z] - this matches any character in the string that is an alphabet from a-z

g - global, to make sure it checks the whole string

i - insensitive, to make sure it doesn't bother about the case of the string
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that we've found how to match the string/characters we want to work on, we should also figure out how to replace them in the string.&lt;/p&gt;

&lt;p&gt;Let's create a function as the second parameter that can take the matched characters in the string and convert them to the new characters we want.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
const newWord = word =&amp;gt; word.toUpperCase().replace(/[^a-z]/gi, '')&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Breakdown of the function above&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;word.toUpperCase() - this transforms the matched characters to uppercase

replace(/[^a-z]/gi, '') - this replaces the transformed characters that aren't alphabets from a-z with empty strings
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  The full method for the solution
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const toCamelCase = str =&amp;gt; {
  return newWord = str.replace(/[^a-z][a-z]/gi, word =&amp;gt; word.toUpperCase().replace(/[^a-z]/gi, ''))
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I'm sure there are several ways to have solved this particular problem, but using the replace method seems very straightforward and easy to use.&lt;br&gt;
You can also comment with your own solutions or questions.&lt;/p&gt;

&lt;p&gt;I hope this helps someone out…&lt;/p&gt;

&lt;p&gt;You can also reach me on &lt;a href="https://twitter.com/The_Oluwajuwon"&gt;Twitter&lt;/a&gt; ✌️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>codewars</category>
      <category>string</category>
    </item>
  </channel>
</rss>
