Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 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.

    Code:
    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
    tj@tj.com

  • #2
    New Coder
    Join Date
    Jul 2002
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •