var str = "Hello world! Welcome to my website.";\n\nvar res = str.split("Welcome").join("Greetings");\n\nconsole.log(res);<\/code><\/pre><\/div>\n\n\n\nOutput: Hello world! Greetings to my website.<\/p>\n\n\n\n
Conclusion<\/h2>\n\n\n\n
In this article, you learned about two methods to replace all spaces in a string with JavaScript. The first method uses the replaceAll() method and the second method uses a regular expression and the replace() method.<\/p>\n\n\n\n
You can also use the split() and join() methods to remove all spaces from a string.<\/p>\n\n\n\n
I hope this article was helpful. Thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"
To replace all spaces in JavaScript, you can use three methods – Use the replaceAll() method passing space as the first argument, and the replacement as the second argument. Use the replace method passing a regex as an argument to replace the spaces. Use the split() and join() methods to replace all spaces. 1. The […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[8],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts\/304"}],"collection":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/comments?post=304"}],"version-history":[{"count":3,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":473,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/posts\/304\/revisions\/473"}],"wp:attachment":[{"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/typedarray.org\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}