0

I would like to get the image name out of the address.

This is the value I with the JavaScript:

http://localhost:51557/img/column-sortable.png

document.getElementById("ctl00_contentHolder_iSortColumn").value = columnNumber;
            alert(imageName);

Whats the best way to get column-sortable.png out of the string?

4

3 回答 3

3

只要 URL 中的图像名称后面没有任何内容(没有查询字符串或哈希),那么以下内容应该有效:

var str = "http://localhost:51557/img/column-sortable.png";
alert(str.substring(str.lastIndexOf('/') + 1));
于 2013-06-17T06:39:04.317 回答
2

请参考拆分功能:

var url = http://localhost:51557/img/column-sortable.png;
var elementArray = url.split('/');
var imageName = elementArray[elementArray.length - 1];

JSFiddle

于 2013-06-17T06:40:43.927 回答
1

如果您想尝试使用 Regex,请查看此内容。

var imgURL = "http://localhost:51557/img/column-sortable.png";
var imageName = imgURL.replace( /^.*?([^/]+\..+?)$/, '$1' );
alert(imageName);
于 2013-06-17T06:46:53.050 回答