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 03-20-2004, 11:02 PM   PM User | #1
Garadon
Regular Coder

 
Join Date: Jul 2002
Posts: 698
Thanks: 0
Thanked 0 Times in 0 Posts
Garadon is an unknown quantity at this point
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.
Garadon is offline   Reply With Quote
Old 03-21-2004, 12:14 AM   PM User | #2
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
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..
Willy Duitt is offline   Reply With Quote
Old 03-21-2004, 07:00 AM   PM User | #3
Garadon
Regular Coder

 
Join Date: Jul 2002
Posts: 698
Thanks: 0
Thanked 0 Times in 0 Posts
Garadon is an unknown quantity at this point
ty it seem to do
Garadon is offline   Reply With Quote
Reply

Bookmarks

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 04:03 AM.


Advertisement
Log in to turn off these ads.