{"id":2161,"date":"2021-09-13T10:09:33","date_gmt":"2021-09-13T10:09:33","guid":{"rendered":"https:\/\/blog.embold.io\/?p=2161"},"modified":"2021-09-14T07:48:21","modified_gmt":"2021-09-14T07:48:21","slug":"how-to-replace-all-occurrences-of-a-string-in-javascript","status":"publish","type":"post","link":"https:\/\/blog.embold.io\/es\/how-to-replace-all-occurrences-of-a-string-in-javascript\/","title":{"rendered":"How to replace all occurrences of a string in JavaScript?"},"content":{"rendered":"\n<p>With the new replaceAll string method, you can replace all occurrences of a string within a string.&nbsp;<\/p>\n\n\n\n<p><strong>Syntax:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string.replaceAll(search, replaceWith)<\/code><\/pre>\n\n\n\n<p>The <code>replaceAll <\/code>method replaces all appearances of the search string with <code>replaceWith <\/code>within the string it is attached to.<\/p>\n\n\n\n<p><strong>Example:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let sentence = \u201cI am alive. But, even if as I am alive, I feel I am not\u201d\nlet newSentence = sentence.replaceAll(\u201cam\u201d, \u201cwas\u201d);\nconsole.log(newSentence)\n\/\/ \u201cI was alive. But, even if as I was alive, I feel I was not\"<\/code><\/pre>\n\n\n\n<p>At the time of writing this, <code>replaceAll <\/code>is currently supported by all modern browsers, according to <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/caniuse.com\/?search=replaceAll\" target=\"_blank\">caniuse<\/a>. So, you should have no problem using it.<\/p>\n\n\n\n<p>However, if you need to support older browsers or the IE browser, then you can try this hacky method using the <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/split\" target=\"_blank\">string.split()<\/a> and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/join\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">array.join()<\/a> methods.&nbsp;<\/p>\n\n\n\n<p>First, use the split method to divide the original string into substrings at every point where the string to be replaced occurs:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let sentence = \u201cI am alive. But, even if as I am alive, I feel I am not\u201d\nlet splitSentence = sentence.split(\u2018am\u2019)\nconsole.log(splitSentence)\n\/\/output: [\"I \", \" alive. But, even if as I \", \" alive, I feel I \", \" not\"]<\/code><\/pre>\n\n\n\n<p>The split method puts the substrings into an array and returns the array.&nbsp;<br><\/p>\n\n\n\n<p>Next, use the join method to join all the substrings together while putting the string to be replaced with, in between:&nbsp;<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Let finalSentence = splitSentence.join('was')\nconsole.log(finalSentence)\n\/\/output: \"I was alive. But, even if as I was alive, I feel I was not\"<\/code><\/pre>\n\n\n<p><span style=\"color: #ff0000\">Blog Credits: Linda Ikechukwu<\/span><\/p>\n<div>\u201d Linda Ikechukwu is a Frontend Developer turned Technical Content Consultant. She enjoys strategizing on and creating content that helps tech startups connect with their target audience. \u201c<br \/><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-2189\" src=\"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/Linda-headshot.jpeg\" alt=\"\" width=\"150\" height=\"210\" srcset=\"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/Linda-headshot.jpeg 715w, https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/Linda-headshot-585x819.jpeg 585w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/div>","protected":false},"excerpt":{"rendered":"<p>With the new replaceAll string method, you can replace all occurrences of a string within a string.&nbsp; Syntax:&nbsp; The replaceAll method replaces all appearances of the search string with replaceWith&hellip;<\/p>\n","protected":false},"author":10,"featured_media":2177,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[200,198,201,199],"better_featured_image":{"id":2177,"alt_text":"","caption":"","description":"","media_type":"image","media_details":{"width":3000,"height":2000,"file":"2021\/09\/embold-blog-replace-string.png","sizes":{"thumbnail":{"file":"embold-blog-replace-string-150x150.png","width":150,"height":150,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-150x150.png"},"medium_large":{"file":"embold-blog-replace-string-768x512.png","width":768,"height":512,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-768x512.png"},"penci-full-thumb":{"file":"embold-blog-replace-string-1170x780.png","width":1170,"height":780,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-1170x780.png"},"penci-slider-thumb":{"file":"embold-blog-replace-string-1170x663.png","width":1170,"height":663,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-1170x663.png"},"penci-magazine-slider":{"file":"embold-blog-replace-string-780x516.png","width":780,"height":516,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-780x516.png"},"penci-slider-full-thumb":{"file":"embold-blog-replace-string-1920x800.png","width":1920,"height":800,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-1920x800.png"},"penci-single-full":{"file":"embold-blog-replace-string-1920x1280.png","width":1920,"height":1280,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-1920x1280.png"},"penci-thumb":{"file":"embold-blog-replace-string-585x390.png","width":585,"height":390,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-585x390.png"},"penci-masonry-thumb":{"file":"embold-blog-replace-string-585x390.png","width":585,"height":390,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-585x390.png"},"penci-thumb-square":{"file":"embold-blog-replace-string-585x585.png","width":585,"height":585,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-585x585.png"},"penci-thumb-vertical":{"file":"embold-blog-replace-string-480x650.png","width":480,"height":650,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-480x650.png"},"penci-thumb-small":{"file":"embold-blog-replace-string-263x175.png","width":263,"height":175,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-263x175.png"},"jr_insta_square":{"file":"embold-blog-replace-string-640x640.png","width":640,"height":640,"mime-type":"image\/png","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string-640x640.png"}},"image_meta":{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0","keywords":[]}},"post":2161,"source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/09\/embold-blog-replace-string.png"},"translation":{"provider":"WPGlobus","version":"2.10.8","language":"es","enabled_languages":["en","es","de","fr","ru"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"es":{"title":false,"content":false,"excerpt":false},"de":{"title":false,"content":false,"excerpt":false},"fr":{"title":false,"content":false,"excerpt":false},"ru":{"title":false,"content":false,"excerpt":false}}},"_links":{"self":[{"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/posts\/2161"}],"collection":[{"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/comments?post=2161"}],"version-history":[{"count":3,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/posts\/2161\/revisions"}],"predecessor-version":[{"id":2192,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/posts\/2161\/revisions\/2192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/media\/2177"}],"wp:attachment":[{"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/media?parent=2161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/categories?post=2161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embold.io\/es\/wp-json\/wp\/v2\/tags?post=2161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}