regex - JavaScript - Regular expression to convert image source -
my question related javascript regular expressions.
i'm building simple lightbox wordpress mootools javascript framework.
wordpress stores pictures in variety of sizes file names like:
'image-50-50x100.jpg' 'image-50-150x100.jpg' 'image-50-1024x698.jpg' 'image-50.jpg'
when user clicks thumbnail image, have convert source of image source of full size image, , preload full-size image.
the question
how change string this:
'http://some-path/image-50-50x100.jpg' 'http://some-path/image-50-150x100.jpg' 'http://some-path/image-50-1024x698.jpg' 'http://some-path/image-50.jpg'
, into:
'http://some-path/image-50.jpg'
missing piece accurate regular-expression in code below:
source.replace( /regular-expression/, '' );
thanks in advance.
this should it:
str = str.replace(/-\d+x\d+/, '');
e.g.:
var str = 'http://some-path/image-50-1024x698.jpg'; str = str.replace(/-\d+x\d+/, ''); console.log(str); // "http://some-path/image-50.jpg"
and case don't want change, doesn't:
var str = 'http://some-path/image-50.jpg'; str = str.replace(/-\d+x\d+/, ''); console.log(str); // "http://some-path/image-50.jpg"
edit: you've said in comment elsewhere that:
in rare cases can happen wordpress user uploads image
image-1024x698.jpg
, wordpress creates thumb imageimage-1024x698-300x300.jpg
okay, add \.
, .
above:
var str = 'http://some-path/image-1024x698-300x300.jpg'; str = str.replace(/-\d+x\d+\./, '.'); console.log(str); // "http://some-path/image-1024x698.jpg"
Comments
Post a Comment