View Full Version : problem with spaces and regular expression

tyler jones
07-04-2002, 02:34 PM
I have this trimming function that is supposed to replace 2 or more instances of spaces with a single space. However, it's also replacing new line characters. Is there a way in regular expressions that I can say, replace 2 or more spaces, but not new line characters? Here's my code. Thanks.

function removeSpaces( str )
var regExp = /\s{2,}/gi;
if ( regExp.test( str )) {
str = str.replace(regExp,' ');
return str;

... and my str may hold the value...

tyler jones
1110 main st nw
st. petersburg, fl 12345

tyler jones
07-04-2002, 02:51 PM
As I looked a bit more closely at what \s actually matches, I noticed in the regular expression documentation that it matches tabs, newlines, carriage returns, etc... So, instead, I just took out the \s and put in the code for tabs, vertical tabs, carriage returns, etc. and it works great.