Web Analytics Made Easy -
StatCounter problem with spaces and regular expression - CodingForum


No announcement yet.

problem with spaces and regular expression

  • Filter
  • Time
  • Show
Clear All
new posts

  • problem with spaces and regular expression

    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
    [email protected]

  • #2
    nevermind, I figured it out

    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.