Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery String Manipulation

I am working on 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::::::
like image 962
tonyf Avatar asked Dec 30 '22 05:12

tonyf


2 Answers

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::::::'
like image 170
Matchu Avatar answered Dec 31 '22 18:12

Matchu


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

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

like image 25
Brian McKenna Avatar answered Dec 31 '22 18:12

Brian McKenna