views:

404

answers:

2

Hi,

Hoping someone can assist with some string manipulation using jQuery.

Basically, within a .click(function(){ I have the following string variable:

f?p=251:1007:3642668879504810:::::

What I need to do using jQuery is basically remove the number 3642668879504810 (which changes, i.e is a random number so cannot match on this number) between the second and third colon within this string variable, so the end result would be as follows, still maintaining all the colons

f?p=251:1007::::::

Any help would be appreciated.

Thanks.

+1  A: 
stringVar = stringVar.replace(/\d+(:+)$/, '$1');

Should work. It finds digits only followed by colons and replaces it with those colons (thereby removing the digits).

Brian McKenna
Took me a while to parse this one in my brain: removes the last number in the string before one or more colons.
Matchu
+4  A: 

A quick way using split():

var str, split_str, new_str;

str = 'f?p=251:1007:3642668879504810:::::';
split_str = str.split(':');
split_str[2] = '';
new_str = split_str.join(':');

// new_str == 'f?p=251:1007::::::'
Matchu
Thanks Matchu but I was just wondering, I've noticed that sometimes before the f?p=251, there might be another value set, such as http://www.google.com/f?p=251:1007:3642668879504810:::::, based on this, is there a away to look for f?p= string and then start from here? Thanks.
tonsils