{"id":7551,"date":"2025-04-01T07:12:52","date_gmt":"2025-04-01T07:12:52","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=7551"},"modified":"2025-04-01T07:24:32","modified_gmt":"2025-04-01T07:24:32","slug":"how-do-i-replace-all-occurrences-of-a-string-in-javascript","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/","title":{"rendered":"How do I replace all occurrences of a string in JavaScript?"},"content":{"rendered":"\n<p>Often web developers need to be able to replace all occurrences of string on their website or application. You can easily do this using JavaScript. In this article, we will learn the different solutions to replace one or more occurrences of a substring in a given string, using plain JavaScript.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#How_do_I_replace_all_occurrences_of_a_string_in_JavaScript\" >How do I replace all occurrences of a string in JavaScript?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#1_Using_ReplaceAll\" >1. Using ReplaceAll<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#2_Using_Replace\" >2. Using Replace<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#3_Using_Split_Join\" >3. Using Split &amp; Join<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_do_I_replace_all_occurrences_of_a_string_in_JavaScript\"><\/span>How do I replace all occurrences of a string in JavaScript?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are the different ways to replace all occurrences of a substring in JavaScript. They can be used to replace multiple occurrences of a single character, word, phrase or expression.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_ReplaceAll\"><\/span>1. Using ReplaceAll<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Every JavaScript string (literal or variable) supports replaceAll() method that allows you to replace all occurrences of substring in JavaScript. Here is its syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">string.replaceAll(new_substring, old_substring)<\/pre>\n\n\n\n<p>In the above function, we need to provide two arguments &#8211; the substring or pattern to be replaced and the new substring to take its place. ReplaceAll() will return a new string with replacement. It will not modify the original string. Here is an example to illustrate this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a= 'good morning, good night';<br>console.log(a.replaceAll('good','hello')); \/\/ output is 'hello morning, hello night'<br>console.log(a); \/\/ output is 'good morning, good night'<\/pre>\n\n\n\n<p>If you specify a string for replacement, then replaceAll() will only replace the exact match of that substring. If you specify a regular expression instead of a string, then it will replace all variations of that regular expression in the original string. This is useful for complex matches. But you need to enclose the regular expression within \/&#8230;\/g<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a= 'good morning, good night';<br>console.log(a.replaceAll(\/good\/g,'hello'));<br>console.log(a); <\/pre>\n\n\n\n<p>If you do not mention the regular expression within \/&#8230;\/g then you will get the following error.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">TypeError: String.prototype.replaceAll called with a non-global RegExp argument<\/pre>\n\n\n\n<p>By default, replaceAll() function does a case-sensitive replacement. If you want to do a case-insensitive replacement, then use \/&#8230;\/gi. The g flag specifies that it is a regular expression, and i flag stands for case insensitive replacement.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a= 'good morning, Good night';<br>console.log(a.replaceAll(\/good\/gi,'hello')); \/\/ output is hello morning, Good night<\/pre>\n\n\n\n<p>Please note, if you are using older web browsers then it may not support replaceAll() function. In such cases, you can use either of the following methods.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_Replace\"><\/span>2. Using Replace<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The replace() method is supported by older web browsers but it only replaces the first occurrence of a given substring, by default.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a= 'good morning, good night';<br>console.log(a.replace('good','hello')); \/\/ output is hello morning, good night<\/pre>\n\n\n\n<p>If you want to replace all occurrences of a substring in a string, then you need to provide a regular expression, instead of a literal string. You can do this by enclosing the substring within \/&#8230;\/g as shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a= 'good morning, good night';<br>console.log(a.replace(\/good\/g,'hello')); \/\/ output is hello morning, hello night<br>console.log(a); \/\/ output is good morning, good night<\/pre>\n\n\n\n<p>Like replaceAll() function, it will not modify the original string but only return modified copy of the original string. If you want to modify the original string, then you need to re-assign the result of replace() function to the same string variable.<\/p>\n\n\n\n<p>Like replaceAll() function, replace() function also does a case sensitive replacement, by default. If you want to do a case insensitive find and replace, then you need to enclose the original substring within \/&#8230;\/gi where g flag means regular expression and i flag means case insensitive match.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a= 'good morning, Good night';<br>console.log(a.replace(\/good\/gi,'hello')); \/\/ output is hello morning, hello night<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_Split_Join\"><\/span>3. Using Split &amp; Join<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This is a little complicated solution but mentioned here for the sake of completeness. In this approach, we use split() function to split the original string into an array of substrings. Then we use join() function to combine the array items into a single string.<\/p>\n\n\n\n<p>The split function basically splits a string into an array of substrings, based on a separator value provided as an argument. In our case, we pass the substring to be replaced as the separator for split function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var a= 'good morning, good night';<br>var arr = a.split('good');<br>console.log(arr); \/\/ output is [ '', ' morning, ', ' night' ]<\/pre>\n\n\n\n<p>On the other hand, join function allows you to concatenate the different items of an array, into a single string. While doing so, you may or may not specify a character or substring to join the items. We will call join function on the result of above split function, and specify the replacement substring to be used during concatenation of array elements.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var b= arr.join('hello');<br>console.log(b); \/\/ output is hello morning, hello night<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt different ways to replace all occurrences of substring in a string, using JavaScript. Among them, using replaceAll() is the simplest and best solution for this purpose. If you are using older web browsers, then you can use replace() function. They are both supported by string literals as well as string variables. By default, both replaceAll() and replace() find and replace exact match of the substring. If you want to do a complex match then you need to use regular expressions instead of literal strings.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-check-whether-string-contains-substring-in-javascript-2\/\">How to Check Whether String Contains Substring in JavaScript<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-remove-empty-elements-from-javascript-array\/\">How to Remove Elements from JavaScript Array<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-detect-invalid-date-in-javascript\/\">How to Detect Invalid Date in JavaScript<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often web developers need to replace all occurrences of string in JavaScript. Here are the different ways to do this.<\/p>\n","protected":false},"author":1,"featured_media":7567,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[295],"tags":[402],"class_list":["post-7551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-replace-all-occurrences"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How do I replace all occurrences of a string in JavaScript? - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Often web developers need to replace all occurrences of string in JavaScript. Here are the different ways to do this.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How do I replace all occurrences of a string in JavaScript? - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Often web developers need to replace all occurrences of string in JavaScript. Here are the different ways to do this.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-01T07:12:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-01T07:24:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/replace-all-occurrences-of-string-javascript.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"260\" \/>\n\t<meta property=\"og:image:height\" content=\"195\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How do I replace all occurrences of a string in JavaScript?\",\"datePublished\":\"2025-04-01T07:12:52+00:00\",\"dateModified\":\"2025-04-01T07:24:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/\"},\"wordCount\":730,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/replace-all-occurrences-of-string-javascript.jpg?fit=260%2C195&ssl=1\",\"keywords\":[\"replace all occurrences\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/\",\"name\":\"How do I replace all occurrences of a string in JavaScript? - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/replace-all-occurrences-of-string-javascript.jpg?fit=260%2C195&ssl=1\",\"datePublished\":\"2025-04-01T07:12:52+00:00\",\"dateModified\":\"2025-04-01T07:24:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Often web developers need to replace all occurrences of string in JavaScript. Here are the different ways to do this.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/replace-all-occurrences-of-string-javascript.jpg?fit=260%2C195&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/replace-all-occurrences-of-string-javascript.jpg?fit=260%2C195&ssl=1\",\"width\":260,\"height\":195,\"caption\":\"replace all occurrences of string in javascript\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How do I replace all occurrences of a string in JavaScript?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How do I replace all occurrences of a string in JavaScript? - Ubiq BI","description":"Often web developers need to replace all occurrences of string in JavaScript. Here are the different ways to do this.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How do I replace all occurrences of a string in JavaScript? - Ubiq BI","og_description":"Often web developers need to replace all occurrences of string in JavaScript. Here are the different ways to do this.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2025-04-01T07:12:52+00:00","article_modified_time":"2025-04-01T07:24:32+00:00","og_image":[{"width":260,"height":195,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/replace-all-occurrences-of-string-javascript.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How do I replace all occurrences of a string in JavaScript?","datePublished":"2025-04-01T07:12:52+00:00","dateModified":"2025-04-01T07:24:32+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/"},"wordCount":730,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/replace-all-occurrences-of-string-javascript.jpg?fit=260%2C195&ssl=1","keywords":["replace all occurrences"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/","url":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/","name":"How do I replace all occurrences of a string in JavaScript? - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/replace-all-occurrences-of-string-javascript.jpg?fit=260%2C195&ssl=1","datePublished":"2025-04-01T07:12:52+00:00","dateModified":"2025-04-01T07:24:32+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Often web developers need to replace all occurrences of string in JavaScript. Here are the different ways to do this.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/replace-all-occurrences-of-string-javascript.jpg?fit=260%2C195&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/replace-all-occurrences-of-string-javascript.jpg?fit=260%2C195&ssl=1","width":260,"height":195,"caption":"replace all occurrences of string in javascript"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-do-i-replace-all-occurrences-of-a-string-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How do I replace all occurrences of a string in JavaScript?"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2025\/04\/replace-all-occurrences-of-string-javascript.jpg?fit=260%2C195&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1XN","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/7551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=7551"}],"version-history":[{"count":19,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/7551\/revisions"}],"predecessor-version":[{"id":7572,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/7551\/revisions\/7572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/7567"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=7551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=7551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=7551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}