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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts

    splitting by spaces

    splitting by spaces:

    I got the following code:

    var test=" v2 v3 v4 v1 ";
    test=test.replace(/^\s+|\s+$/g,'');
    test=test.replace(/([\s]+\s)/g,' ');
    test=test.split(' ');
    alert(test)


    basicly the 1st replace remove spaces on eeach side of the string
    2nd replace takes groups spaces inside the string an replace with 1 space.
    the split make an array splitting the string by space.

    array(v2,v3,v4,v1)

    my question is are there a better regexp that can achieve the same goal on 1 line that I use 3 lines to.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm still struggling with Regular Expressions myself.
    But this should work.
    Code:
    <script type="text/javascript">
    var test=" v2 v3        v4 v1 ";
    test=test.replace(/^\s+|\s(?=\s)|\s+$/g,'').split(/\s/);
    document.write(test)
    </script>
    .....Willy
    Last edited by Willy Duitt; 03-21-2004 at 12:17 AM.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ty it seem to do


  •  

    Posting Permissions

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