{"id":304,"date":"2022-06-01T02:21:55","date_gmt":"2022-06-01T02:21:55","guid":{"rendered":"https:\/\/typedarray.org\/?p=304"},"modified":"2022-06-05T12:04:09","modified_gmt":"2022-06-05T12:04:09","slug":"replace-all-spaces-in-javascript","status":"publish","type":"post","link":"https:\/\/typedarray.org\/replace-all-spaces-in-javascript\/","title":{"rendered":"Replace All Spaces In JavaScript"},"content":{"rendered":"\n

To replace all spaces in JavaScript, you can use three methods –<\/p>\n\n\n\n

  1. Use the replaceAll() method passing space as the first argument, and the replacement as the second argument.<\/li>
  2. Use the replace method passing a regex as an argument to replace the spaces.<\/li>
  3. Use the split() and join() methods to replace all spaces.<\/li><\/ol>\n\n\n
    \n
    \"how<\/figure><\/div>\n\n\n

    1. The replaceAll() Method To Replace All Spaces In JavaScript<\/h2>\n\n\n\n

    Passing space as the first argument and the replacement as the second argument to the replaceAll() method, all spaces in JavaScript can be replaced with a specific character or string.<\/p>\n\n\n\n

    Example<\/strong>:<\/p>\n\n\n\n

    replaceAll( ” “, “_” ); \/\/ replace all spaces with underscore<\/p>\n\n\n\n

    replaceAll( ” “, “” ); \/\/ remove all spaces<\/p>\n\n\n\n

    Here’s a complete example to replace all spaces in a string with dash using the replaceAll() method:<\/p>\n\n\n\n

    var str = "Hello World!";\n\nvar res = str.replaceAll( " ", "-" );\n\nconsole.log(res); \/\/ Hello-World!<\/code><\/pre><\/div>\n\n\n\n

    2. Use A Regex And replace() Method To Replace All Spaces In JavaScript<\/h2>\n\n\n\n

    Another way to replace all spaces in JavaScript is using a regular expression. The replace() method accepts a regex as an argument and replaces all occurrences of the given regex with the replacement string.<\/p>\n\n\n\n

    Example<\/strong>:<\/p>\n\n\n\n

    replace(\/ \/g, ‘-‘); \/\/ replace all spaces with dash<\/p>\n\n\n\n

    replace(\/ \/g, ”); \/\/ remove all spaces<\/p>\n\n\n\n

    Here’s a complete example to replace all space characters with dashes using a regular expression and the replace() method:<\/p>\n\n\n\n

    var str = "Hello World!";\n\nvar res = str.replace(\/ \/g, '-');\n\nconsole.log(res); \/\/ Hello-World!<\/code><\/pre><\/div>\n\n\n\n

    3. Use split() And join() Methods To Replace All Spaces In JavaScript<\/h2>\n\n\n\n

    Another way to replace all spaces in JavaScript is using the split() method which splits a string into an array of substrings, and the join() method that joins all element of an array into a string.<\/p>\n\n\n\n

    Example<\/strong>:<\/p>\n\n\n\n

    split(‘ ‘).join(‘-‘); \/\/ replace all spaces with dash<\/p>\n\n\n\n

    split(‘ ‘).join(”); \/\/ remove all spaces<\/p>\n\n\n\n

    Here’s a complete example to replace all spaces with dashes using the split() and join() methods:<\/p>\n\n\n\n

    var str = "Hello World!";\n\nvar res = str.split(' ').join('-');\n\nconsole.log(res); \/\/ Hello-World!<\/code><\/pre><\/div>\n\n\n\n

    Let’s look at these methods in detail below.<\/p>\n\n\n\n

    replaceAll() Method In JavaScript<\/h2>\n\n\n\n

    The replaceAll()<\/a> method takes two parameters –<\/p>\n\n\n\n