homepage Welcome to WebmasterWorld Guest from 54.205.168.88
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
split() and pop()
separator and end of line
nyteshade




msg:4423216
 7:06 pm on Feb 29, 2012 (gmt 0)

I'm learning to use String methods with RegExp object. I have:

mystr = 'Cinnamon, 1223, Peach, 1224, Jasmine, 1225, Pumpkin, 1226, Apple, 1227';

myre = /[^a-z]+/i; //split on

var arr = mystr.split(myre);

console.log(arr):
["Cinnamon", "Peach", "Jasmine", "Pumpkin", "Apple", ""]


Notice the empty element thingy, the output I want is:

["Cinnamon", "Peach", "Jasmine", "Pumpkin", "Apple"]


So I've tried this:


arr = mystr.split(/[^a-z]+/i);
arr.pop();

console.log(arr);
//["Cinnamon", "Peach", "Jasmine", "Pumpkin", "Apple"]


Which gives me what I want but my question is: is this the accepted way to handle a str.split() when the 'separator' is at the end of the string? I'm assuming this must happen often enough to warrant a standard way.

One last thing, I tried doing:


arr = mystr.split(/[^a-z]+/i).pop();


As shown in this forum and other places, but when I look in console.log it tells me 'empty string'. Thanks all.

 

daveVk




msg:4423437
 3:32 am on Mar 1, 2012 (gmt 0)

arr = mystr.split(/[^a-z]+/i).pop()


is equiv to

var temp = mystr.split(/[^a-z]+/i);
arr = temp.pop();

the last item is removed from temp.
pop() returns the removed item, hence empty string.

nyteshade




msg:4423702
 5:15 pm on Mar 1, 2012 (gmt 0)

Thanks daveVk. That makes sense, if there was a way to 'walk thru' each regular expression step in Firefox Firebug then that would help (btw, is there?).

So is there a way to make...


arr = mystr.split(/[^a-z]+/i);
arr.pop();


... an elegant one-liner?

daveVk




msg:4423969
 3:00 am on Mar 2, 2012 (gmt 0)

( arr = mystr.split(/[^a-z]+/i) ).pop();

would probably work, elegant I wonder ?

On a tangent, this regEx look a bit fragile, eg "blue berry" comes out as "blue" and "berry".

nyteshade




msg:4424049
 9:27 am on Mar 2, 2012 (gmt 0)

( arr = mystr.split(/[^a-z]+/i) ).pop();

Works, thanks; and just needed to add a space in the regex to fix the Blue Berry.


mystr = 'Cinnamon, 1223, Peach, 1224, Jasmine, 1225, Pumpkin, 1226, Apple Spice, 1227, Blue Berry, 0420';
myre = /[^a-z]+ /i;//split on

//gives
["Cinnamon", "Peach", "Jasmine", "Pumpkin", "Apple Spice", "Blue Berry"]

;)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved