Solved

splitting string and checking that segments exist

Posted on 2011-09-14
20
234 Views
Last Modified: 2012-05-12
If i have a string like "purchase_click_AmazonUK_XBOX360" 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"
0
Comment
Question by:894359
  • 10
  • 9
20 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 36539266
something like this?
<html>
<body>

<script type="text/javascript">

var s = "purchase_click"
var n = s.split( '_' )
if ( n.length < 3 ) {
  n.push( 'None' )
}
if ( n.length < 4 ) {
  n.push( 'None' )
}
document.write( n )

</script>

</body>
</html>

Open in new window

0
 

Author Comment

by:894359
ID: 36539346
I actually only want the last 2 values combined to make a new string with a ":"  so for example:

"purchase_click_AmazonUK_XBOX360" --> "AmazonUK:XBOX360"

"purchase_click_AmazonUK" --> "AmazonUK:NONE"

"purchase_click" --> "NONE:NONE"
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36539568
use : str.split(/purchase_click|_/).slice(2).concat(["NONE","NONE"]).slice(0,2).join(":")

test page : http://jsfiddle.net/QyVQp/
str = "purchase_click_AmazonUK_XBOX360"; // --> "AmazonUK:XBOX360"
str = str.split(/purchase_click|_/).slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

str = "purchase_click_AmazonUK"; // --> "AmazonUK:NONE"
str = str.split(/purchase_click|_/).slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

str = "purchase_click"; // --> "NONE:NONE"
str = str.split(/purchase_click|_/).slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:894359
ID: 36539807
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
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36539851
>Every example came out "NONE:NONE"

with the test page? incredible!
0
 

Author Comment

by:894359
ID: 36539968
You lost me?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540006
click on the link and confirm you get each time NONE:NONE,
thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540111
0
 

Author Comment

by:894359
ID: 36540122
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!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540133
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
0
 

Author Comment

by:894359
ID: 36540140
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?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540146
try with firefox, the error you get come from jsfiddle not from the script itself
0
 

Author Comment

by:894359
ID: 36540181
I get the proper results in FF but not IE.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540190
Wouldn't be "word underscore word underscore" ?

one of these word can be empty?

for example :

underscore word underscore

or again :

underscore underscore

0
 

Author Comment

by:894359
ID: 36540194
Just changed to this and it worked:

str = "purchase_click_AmazonUK_XBOX360"; // --> "AmazonUK:XBOX360"
str = str.split("_").slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

str = "purchase_click_AmazonUK"; // --> "AmazonUK:NONE"
str = str.split("_").slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);

str = "purchase_click"; // --> "NONE:NONE"
str = str.split("_").slice(2).concat(["NONE","NONE"]).slice(0,2).join(":");
alert(str);
0
 

Author Comment

by:894359
ID: 36540196
>Wouldn't be "word underscore word underscore" ?

No it will always end with a word and never an underscore.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540208
>Just changed to this and it worked:

so happy end?!
0
 

Author Comment

by:894359
ID: 36540230
Yes for now! :0)
0
 

Author Closing Comment

by:894359
ID: 36540233
Please see corrections for IE in thread.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36540234
^^;
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now