I wrote a very basic jquery script to obtain the src attribute from my img elements. Long story short, based on the number contained in the src attribute of my thumbnails, the code then uses that same id number to fetch the corresponding main image:
sample thumb src value: catalog/p1-t1.png | where p = piece and 1 = id, t1 = thumb 1 sample mainpic src value: catalog/p1-开发者_运维知识库1.png | where p = piece and 1 = id, 2nd 1 = main pic for thumb 1
It all worked nicely up until single digit numbers became double digits and thus, using something like:
$string = $string.substring(12,13);
ceased to work (obvious yes).
I want to get from my thumb's 'catalog/p1-t1.png' src value the '1' after the 't', whether its t1 or t33 or t999.
Thanks in advance G.Campos
A wild Regular Expression appears!
var string = 'catalog/p1-t666.png',
re = /\/p(\d+)-t(\d+)/
result = re.exec(string),
piece = result[1], // 1
thumb = result[2]; // 666
Oh and later a slightly less wild demo appears, I guess.
You can use a minimal regular expression if you first split on the period '.':
var x = 'p1-t123.jpg';
alert(x.split('.')[0].match(/\d+$/));
Presuming that the image path only has one period. If not, you can likely trim the trailing stuff after the last period then grab the trailing digits:
alert( x.replace(/[^\d]+$/,'').match(/\d+$/));
精彩评论