开发者

jQuery String Manipulation

开发者 https://www.devze.com 2022-12-16 21:04 出处:网络
I am working on some string manipulation using jQuery.开发者_开发百科 Basically, within a .click(function(){ I have the following string variable:

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::::::


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::::::'


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号