Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 09:50 PM   PM User | #1
zep
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
zep is an unknown quantity at this point
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.
zep is offline   Reply With Quote
Old 02-01-2013, 10:12 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 946
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is online now   Reply With Quote
Old 02-01-2013, 11:25 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,193
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 02-02-2013, 01:44 PM   PM User | #4
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
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..
007julien is offline   Reply With Quote
Old 02-02-2013, 02:34 PM   PM User | #5
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 359
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
@ 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.
Airblader is online now   Reply With Quote
Reply

Bookmarks

Tags
regexp, split, string

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:24 PM.


Advertisement
Log in to turn off these ads.