-1

I want to extract the url to download a file when user clicks on the image. the url of the image contains the url of the document as /Image/_Z_/Catalog_doc.jpg

Then user clicks I call a jQuery function fetchAndOpen(this)

In this method I would like to build proper file url i.e. : /Docs/Catalog.doc

So basically my issue is image extension (.jpg or .png or any other) second thing I want to remove /_Z_/ (this is hard coded predefined) and the last thing I want to replace _ with . with the document extension.

Is there any regex or some simple solution of jQuery that can help me i know this can be easily done in javascript with substring and lastIndexOf methods but i expecting some thing with jquery / regex or other jquery based way out.


INPUT: /Image/Z/Catalog_doc.jpg -> OUTPUT: /Docs/Catalog.doc

Thanks.

4

1 回答 1

0

给定输入:/Image/ Z /Catalog_doc.jpg 预期输出:/Docs/Catalog.doc

我只是将这个过程分解为多个步骤,以展示从你拥有的东西到你想要的东西的转变。如果您愿意,您可以在正则表达式中执行此操作,但此过程很容易绘制出来,并且考虑到它不会作为一个耗时的过程发生,您无需担心执行多个步骤的性能。

http://jsfiddle.net/cgEt3/

var input = '/Image/_Z_/Catalog_doc.jpg';
var output = input.split('/');
output = output[output.length-1]; // get the last part, now output is Catalog_doc.jpg
var ext = output.lastIndexOf('.');
output = output.substring(0, ext); // now just 'catalog_doc';
ext = output.lastIndexOf('_');
output = output.substring(0, ext) + '.' + output.substring(ext+1); // now 'catalog.doc';
output = '/Docs/' + output;

document.write(output);
于 2013-02-06T18:54:19.873 回答