Here is a sample that uses regular expressions to replicate the functionality of VBScript's *trim functions in JavaScript:
<script language=jscript runat=server> function ltrim(str) { return str.replace(/^[ ]+/, ''); } function rtrim(str) { return str.replace(/[ ]+$/, ''); } function trim(str) { return ltrim(rtrim(str)); } var foo = " bar "; Response.Write("_" + ltrim(foo) + "_<br>\n"); Response.Write("_" + rtrim(foo) + "_<br>\n"); Response.Write("_" + trim(foo) + "_"); </script> |
While the HTML might look like it trimmed too much in the first two cases, if you view the *source* of the output, you will see that all of the spaces are intact.