{"id":2154,"date":"2022-08-30T15:22:30","date_gmt":"2022-08-30T15:22:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/how-to-replace-an-apostrophe-in-a-string-in-javascript-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:30","modified_gmt":"2022-08-30T15:22:30","slug":"how-to-replace-an-apostrophe-in-a-string-in-javascript-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-replace-an-apostrophe-in-a-string-in-javascript-collection-of-common-programming-errors\/","title":{"rendered":"How to replace an apostrophe in a string in Javascript?-Collection of common programming errors"},"content":{"rendered":"<pre><code>var str = \"this's kelly\"\nstr = str.replace(\/'\/g, 'A');\n<\/code><\/pre>\n<p>The reason your version wasn&#8217;t working is because <code>str.replace<\/code> returns the new string, without updating in place.<\/p>\n<p>I&#8217;ve also updated it to use the regular expression version of <code>str.replace<\/code>, which when combined with the <code>g<\/code> option replaces all instances, not just the first. If you actually wanted it to just replace the first, either remove the <code>g<\/code> or do <code>str = str.replace(\"'\", 'A');<\/code><\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-04 00:53:07. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>var str = &#8220;this&#8217;s kelly&#8221; str = str.replace(\/&#8217;\/g, &#8216;A&#8217;); The reason your version wasn&#8217;t working is because str.replace returns the new string, without updating in place. I&#8217;ve also updated it to use the regular expression version of str.replace, which when combined with the g option replaces all instances, not just the first. If you actually [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2154","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2154"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2154\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}