View Full Version : Javascript Form Spaces Question

Sep 6th, 2007, 05:40 AM
Hi, I have a Javscript form input question. I am assuming the Ltrim/Rtrim functions would be suitable for what I am trying to do? In the following code, does anyone know how after the search and replace scheme has been completed, the blank spaces can be removed (or at least the space at the beginning)?




<script type = "text/javascript">

function replaceChars() {
var temp = document.subform.text.value;
temp = ' ' + temp + ' ';

temp = temp.replace(/\shello\s/gi, " goodbye ");
temp = temp.replace (/\sthis\s/gi, " that ");
temp = temp.replace (/\stoday\s/gi, " tomorrow ");

document.subform.text.value = temp;




<form name="subform">

<textarea rows="8" name="text" cols="31" value=""></textarea>
<input type=button name=action value="Change" onClick="replaceChars(document.subform.text.value);">


Philip M
Sep 6th, 2007, 08:24 AM
temp = " xxx yyy zzz "
temp = temp replace(/\s/g, "") // remove all spaces
temp = temp.replace (/^\s+/, "") //. remove leading space(s)
temp = temp.replace ((^\s+)(\s+$), "") remove leading and trailing spaces

Posts by user:23

You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html

Sep 7th, 2007, 12:22 AM
Oh nice, thank you so much!