MJ
asked on
splitting string and checking that segments exist
If i have a string like "purchase_click_AmazonUK_X BOX360" I want to split it and grab the last 2 values (AmazonUK and XBOX360 in this case) but I also need to check if something exists in these positions and if not give a placeholder value of "NONE"
ASKER
I actually only want the last 2 values combined to make a new string with a ":" so for example:
"purchase_click_AmazonUK_X BOX360" --> "AmazonUK:XBOX360"
"purchase_click_AmazonUK" --> "AmazonUK:NONE"
"purchase_click" --> "NONE:NONE"
"purchase_click_AmazonUK_X
"purchase_click_AmazonUK" --> "AmazonUK:NONE"
"purchase_click" --> "NONE:NONE"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Leakim,
I can't assume that the first part of the string will always be "purchase_click" it could be other values. Also I wasn't seeing the results I expected from the code sample you provided? Every example came out "NONE:NONE"
Thanks,
MJ
I can't assume that the first part of the string will always be "purchase_click" it could be other values. Also I wasn't seeing the results I expected from the code sample you provided? Every example came out "NONE:NONE"
Thanks,
MJ
ASKER
You lost me?
click on the link and confirm you get each time NONE:NONE,
thanks
thanks
ASKER
No... I got the expected values. I still can't use the string "purchase_click" as it may change but there will always be a pattern of "word underscore word"
Thanks!
Thanks!
try with firefox, the error you get come from jsfiddle not from the script itself
once you confirm it work (because it use standard javascript function) we can do additional works
once you confirm it work (because it use standard javascript function) we can do additional works
ASKER
One weird thing is when I copy your code and run it locally I get all "NONE" as mentioned but the same code returns different results on your example page? I have no idea why?
try with firefox, the error you get come from jsfiddle not from the script itself
ASKER
I get the proper results in FF but not IE.
Wouldn't be "word underscore word underscore" ?
one of these word can be empty?
for example :
underscore word underscore
or again :
underscore underscore
one of these word can be empty?
for example :
underscore word underscore
or again :
underscore underscore
ASKER
Just changed to this and it worked:
str = "purchase_click_AmazonUK_X BOX360"; // --> "AmazonUK:XBOX360"
str = str.split("_").slice(2).co ncat(["NON E","NONE"] ).slice(0, 2).join(": ");
alert(str);
str = "purchase_click_AmazonUK"; // --> "AmazonUK:NONE"
str = str.split("_").slice(2).co ncat(["NON E","NONE"] ).slice(0, 2).join(": ");
alert(str);
str = "purchase_click"; // --> "NONE:NONE"
str = str.split("_").slice(2).co ncat(["NON E","NONE"] ).slice(0, 2).join(": ");
alert(str);
str = "purchase_click_AmazonUK_X
str = str.split("_").slice(2).co
alert(str);
str = "purchase_click_AmazonUK";
str = str.split("_").slice(2).co
alert(str);
str = "purchase_click"; // --> "NONE:NONE"
str = str.split("_").slice(2).co
alert(str);
ASKER
>Wouldn't be "word underscore word underscore" ?
No it will always end with a word and never an underscore.
No it will always end with a word and never an underscore.
>Just changed to this and it worked:
so happy end?!
so happy end?!
ASKER
Yes for now! :0)
ASKER
Please see corrections for IE in thread.
^^;
Open in new window