Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Replace Multiple Spaces with Single Space</title> <script> var myStr = 'The quick brown fox'; document.write('<pre>' + myStr + '</pre>'); var newStr = myStr.replace(/ +/g, ' '); document.write('<pre>' + newStr + '</pre>'); </script> </head> <body> <!--Strings printed here--> </body> </html>