Solved

splitting string and checking that segments exist

Posted on 2011-09-14
20
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

729 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