我有以下图片src:
<img src="http://xyz.example.com/abc/def/ghi/someimagename.jpg">
如何在没有扩展名的情况下使用jquery提取图像名称,例如在上述情况下,这将是someimagename
?
答案 0 :(得分:1)
您可以使用正则表达式/\/([^\/]+?)\.jpg">/
并获取捕获组值
var str = '<img src="http://xyz.example.com/abc/def/ghi/someimagename.jpg">';
var res = str.match(/\/([^\/]+)\.jpg">/)[1];
document.write(res);
答案 1 :(得分:0)
你也可以这样做。
var string = '<img src="http://xyz.example.com/abc/def/ghi/someimagename.jpg">';
function reverse(s){
return s.split("").reverse().join("");
}
string = reverse(string).split(".")[1].split("/")[0];
string = reverse(string);
编辑:
或者您可以执行以下操作,这对任何类型的网址都非常有用
struct MarkerDistance
{
std::string linkName;
double distance;
MarkerDistance(std::string a, double b)
{
linkName = a;
distance = b;
}
//Version 1
cv::FileStorage & MarkerDistance::operator<< (cv::FileStorage & fs)
{
std::string link = "linkName : " + linkName + "\n";
std::string dist = "distance : " + std::to_string(distance) + "\n";
std::string erg = link + dist;
fs << erg;
}
//Version 2
std::ostream& MarkerDistance::operator << (std::ostream& os)
{
// write obj to stream
std::string link = "linkName : " + linkName + "\n";
std::string dist = "distance : " + std::to_string(distance) + "\n";
std::string erg = link + dist;
return os << erg;
}
};