<?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: learninglead</title>
    <description>The latest articles on DEV Community by learninglead (@learninglead).</description>
    <link>https://dev.to/learninglead</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%2F1067563%2F0a823282-94b0-40e4-b6ac-e3d4fe7b90ab.png</url>
      <title>DEV Community: learninglead</title>
      <link>https://dev.to/learninglead</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/learninglead"/>
    <language>en</language>
    <item>
      <title>How to Replace All String Occurrences in JavaScript</title>
      <dc:creator>learninglead</dc:creator>
      <pubDate>Fri, 21 Apr 2023 07:11:02 +0000</pubDate>
      <link>https://dev.to/learninglead/how-to-replace-all-string-occurrences-in-javascript-4407</link>
      <guid>https://dev.to/learninglead/how-to-replace-all-string-occurrences-in-javascript-4407</guid>
      <description>&lt;p&gt;&lt;strong&gt;To replace all string occurrences in JavaScript, use String.replace() method&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;String.replace()&lt;/code&gt; method returns a new string with one, some, or all matches of a pattern replaced by a replacement. &lt;/p&gt;

&lt;p&gt;The pattern can be a &lt;strong&gt;string or a regular expression (RegExp)&lt;/strong&gt;, and the replacement can be a string or a function called for each match. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note that If the pattern is a string, only the first match will be replaced.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = "duck duck go";
const result = str.replace('duck', 'dove');

console.log(result); // 👉️ dove duck go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the method &lt;code&gt;String.replace()&lt;/code&gt; replaced only the first match.&lt;/p&gt;

&lt;h2&gt;
  
  
  So how can we replace all the matches?
&lt;/h2&gt;

&lt;p&gt;Using &lt;strong&gt;regular expression(RegEx)&lt;/strong&gt;, we can remove all the matches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = "duck duck go";
const regEx = /duck/;
const result = str.replace(regEx, 'dove');
console.log(result); // 👉️ dove dove go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we used regex pattern matching to replace all strings.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note that we append the g (global) flag to the regex pattern, which means global.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;g&lt;/code&gt; flag is not appended, it &lt;strong&gt;replaces only the first occurrence&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = "duck duck go";
const regEx = /duck/;
const result = str.replace(regEx, 'dove');

console.log(result); // 👉️ dove duck go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, by adding the &lt;code&gt;i&lt;/code&gt; flag, you can make** case insensitive replacements**.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = "Duck duck go";
const regEx = /duck/gi;
const result = str.replace(regEx, 'dove');

console.log(result); // 👉️ dove dove go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use regex to &lt;a href="https://stackthrive.com/javascript-remove-special-characters-from-string/"&gt;remove all special characters from the string&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = "duck@duck#go";
const regEx = /[^a-zA-Z0-9]/g;
const result = str.replace(regEx,' ');

console.log(result); // 👉️ dove dove go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, we can easily replace all string occurrences, but I suggest using &lt;code&gt;String.replace()&lt;/code&gt; method.&lt;/p&gt;

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