开发者

strip part of a filename with jquery/js

I am using the code below to send an ID to my page:

The image names will always look like this 12.jpg 12-1.jpg 12-2.jpg 12-3.jpg e.t.c

I need to alter the line below so it will only send the 12 not the -1,-2,-3 e.t.c My code below already removes the .jpg part

var id = $(th开发者_如何学JAVAis).attr('src').split('/').pop().replace('.jpg','');


Remove trailing hyphen separated parts:

id= id.split('-')[0]


var id = $(this).attr('src').split('/').pop().replace('.jpg','');
var hyphenIndex = id.indexOf('-');
id = hyphenIndex > 0 ? id.substring(0, hyphenIndex) : id;


How about a regex instead?

var id = $(this).attr('src').replace( /(-\d+)?\.jpg/, '' );


like bobince, this should work...

var id = $(this).attr('src').split('/').pop().split('-')[0].replace('.jpg','');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜