原文地址 http://www.devcurry.com/2010/11/remove-all-white-space-using-javascript.html
<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Remove Whitespaces using JavaScript by DevCurry.com</title> <script type="text/javascript"> function removeWhitespaces() { var txtbox = document.getElementById('txtOne'); txtbox.value = txtbox.value.replace(/\s/g, ""); } </script> </head> <body> <input id="txtOne" type="text" /><br /> <input id="btnRemoveWs" type="submit" value="Remove Whitespaces" onclick="removeWhitespaces()" /> </body> </html>
The "g" stands for "global" which replaces all white space matches (front, trailing and in between)