Solved

splitting string and checking that segments exist

Posted on 2011-09-14
20
229 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

706 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

15 Experts available now in Live!

Get 1:1 Help Now