{"id":832,"date":"2014-10-28T07:07:05","date_gmt":"2014-10-28T11:07:05","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=832"},"modified":"2024-03-02T16:02:09","modified_gmt":"2024-03-02T21:02:09","slug":"string-replace-in-javascript","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2014\/10\/28\/string-replace-in-javascript\/","title":{"rendered":"String Replace in JavaScript"},"content":{"rendered":"<p>Today, I had the need to do a String.Replace in JavaScript.  I thought, &#8220;surely, this is an easily solved problem&#8221;.  It turns out that I was both right and wrong.<\/p>\n<p>A quick DuckDuckGo search brought back that I could do something like this:<\/p>\n<pre>\r\nvar input = \"I hate Fridays.  Fridays are the worst!\";\r\nvar output = input.replace(\"Fridays\", \"Mondays\");\r\n<\/pre>\n<p>I would expect the value of output to be &#8220;I hate Mondays.  Mondays are the worst!&#8221;.  But it isn&#8217;t.  Instead, the value is &#8220;I hate Mondays. Fridays are the worst!&#8221;, because the replace method will only replace the first instance of the match.<\/p>\n<p>The first solution that I found to this was to modify the code to use a regular expression, which the replace method also takes.  All you have to do is include the RegEx and the &#8220;g&#8221; flag to make the replacement global.  That would make the code look like this:<\/p>\n<pre>\r\nvar input = \"I hate Fridays.  Fridays are the worst!\";\r\nvar output = input.replace(\/Fridays\/g, \"Mondays\");\r\n<\/pre>\n<p>This does give us the correct output of &#8220;I hate Mondays. Mondays are the worst!&#8221;, but if this were to be a reusable method or a pattern throughout an application, getting the correct RegEx can be tricky to debug or come back to later.  Not everyone is a RegExpert.  That&#8217;s why the next solution that I found was ingenious and was the one that I ended up going with.<\/p>\n<p>In this code, you split the string on your &#8220;outgoing&#8221; string, and join it back together again with your &#8220;incoming&#8221; string.  That would look like this:<\/p>\n<pre>\r\nvar input = \"I hate Fridays.  Fridays are the worst!\";\r\nvar output = input.split(\"Fridays\").join(\"Mondays\");\r\n<\/pre>\n<p>That gives us our correct output of &#8220;I hate Mondays. Mondays are the worst!&#8221;.  This method can be slower in some browsers (<a href=\"http:\/\/jsperf.com\/test-join-and-split\/3\">This JSPerf<\/a> test has Split\/Join as coming out ~40% slower in Chrome), but if you aren&#8217;t doing this several hundred times in a loop, I think the readability and maintainability of the Split\/Join way makes this easy to do.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I had the need to do a String.Replace in JavaScript. I thought, &#8220;surely, this is an easily solved problem&#8221;. It turns out that I was both right and wrong. A quick DuckDuckGo search brought &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[135],"class_list":["post-832","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=832"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/832\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}