• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

splitting string and checking that segments exist

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
894359
Asked:
894359
  • 10
  • 9
1 Solution
 
HonorGodCommented:
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
 
894359Author Commented:
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
 
leakim971PluritechnicianCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
894359Author Commented:
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
 
leakim971PluritechnicianCommented:
>Every example came out "NONE:NONE"

with the test page? incredible!
0
 
894359Author Commented:
You lost me?
0
 
leakim971PluritechnicianCommented:
click on the link and confirm you get each time NONE:NONE,
thanks
0
 
leakim971PluritechnicianCommented:
0
 
894359Author Commented:
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
 
leakim971PluritechnicianCommented:
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
 
894359Author Commented:
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
 
leakim971PluritechnicianCommented:
try with firefox, the error you get come from jsfiddle not from the script itself
0
 
894359Author Commented:
I get the proper results in FF but not IE.
0
 
leakim971PluritechnicianCommented:
Wouldn't be "word underscore word underscore" ?

one of these word can be empty?

for example :

underscore word underscore

or again :

underscore underscore

0
 
894359Author Commented:
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
 
894359Author Commented:
>Wouldn't be "word underscore word underscore" ?

No it will always end with a word and never an underscore.
0
 
leakim971PluritechnicianCommented:
>Just changed to this and it worked:

so happy end?!
0
 
894359Author Commented:
Yes for now! :0)
0
 
894359Author Commented:
Please see corrections for IE in thread.
0
 
leakim971PluritechnicianCommented:
^^;
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now