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 5 of 5
  1. #1
    zep
    zep is offline
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    string split RegExp problem

    Hi.

    I´d like to get an array from a split that´d remove all whitespaces from words, and return me only the words. Example:

    var string = " test1 test2 test3 test4 ";

    So I would want a regexp on split that´d remove all the whitespaces, and return only the words.

    The array returned would be like this ["test1", "test2", "test3", "test4"].

    Actually the string have more than one (various) white spaces between the words.
    The Editor text of this forum cut them out.

    Thanks.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Split on " " (space).. that's it.. you might have some array elements that are empty, but the ones that are not will be the words you seek.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,542
    Thanks
    77
    Thanked 4,382 Times in 4,347 Posts
    Yes, but it's easier overall to do as he requested, so that he doesn't have to worry about any empty elements.

    Trivial:
    Code:
    var s = "               test1 test2 test3                                  test4 ";
    
    var arr = s.replace(/\s+/g, " ").replace(/(^\s|\s$)/g,"").split(" ");
    First, reduce all multiple spaces to one space. Then trim the space (if any) off the front and back of the string. Then split on space.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Or simply

    Code:
    var string = " test1 test2 test3 test4 ";
    var arr = string.replace(/(^\s|\s$)/g,"").split(/\s+/g);
    Last edited by 007julien; 02-02-2013 at 01:48 PM.

  • #5
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    @ 007julien

    Actually even better would be

    Code:
    var string = " test1 test2 test3   test4 test5    test6    test7  ";
    var arr = string.replace(/(^\s+|\s+$)/g,"").split(/\s+/g);
    so it can also deal with multiple spaces at the beginning or end of the string.

    However, as a general rule, when questions like this one come up I like to ask what it's for because this kind of thing might be an indication for bad design.


  •  

    Tags for this Thread

    Posting Permissions

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