开发者

Regex to strip image dimentions 125x125

I have a directory in which I store images the directory is ALWAYS 125x125 or 468x60 or various different sizes of width开发者_开发百科 before the x and various heights after the x

I have a constant

x

imgheight = imgdimentions.replace(/(.*?)x/ig, '');

if I use the above regex I can get the image height.

How can I get the image width - The part before the X

imgwidth = imgdimentions.replace(/x(.*?)/ig, '');

I have tried the above but it gives me a error

Thanks


var img_dimensions = imgdimentions.split("x");
var img_width      = img_dimensions[0];
var img_height     = img_dimensions[1];

What you meant to do was

var imgheight = imgdimentions.replace(/.*x/, '');
var imgwidth  = imgdimentions.replace(/x.*/, '');

but that's needlessly complicated in comparison to just using split(). Note that neither the .*? reluctant quantifier nor the parentheses nor the ig modifiers are actually necessary for the regexes here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜