I want to find all anchor tags that have href
attributes that end with some specific image extensions. I want to replace them by image tags.
I've tried the following, but it doesn't match any anchor:
var imageTypes = ["jpeg", "jpg", "gif", "png", "bmp", "tif"];
$(".tfeedpost-postcontent a").each(function () {
var actualA = $(this);
var actualHref = $(this).attr("href");
if (actualHref.match(imageTypes)) {
actualA.replaceWith("<img src='" + actualHref + "'>");
}
});
And I've tried this too:
if (actualHref.match(imageTypes + "$")) {
//...
}
If I put the following, it works, but obviously only with one type of image.
if (actualHref.match("jpg$")) {
//...
}
How can I have a match for any of the extensions?
答案 0 :(得分:2)
您可以使用管道运算符(在正则表达式中用作OR),并要求它前面带一个点并出现在字符串的末尾($):
if (actualHref.match(/\.(jpeg|jpg|gif|png|bmp|tif)$/)) {
....
}
以下扩展程序还将正确处理具有类似图片扩展名的参数或哈希值的网址
if (actualHref.match(/^[^?#]*\.(jpeg|jpg|gif|png|bmp|tif)($|[?#])/)) { ...
这个表达式要求没有&#34;?&#34;或&#34;#&#34;在匹配的扩展名之前发生,并允许其中一个出现在它之后。
答案 1 :(得分:1)
以下将做你想做的事:
<?php
$string="92.14";
$numericstring=floatval(trim($string));
echo $numericstring;
?>
编辑:更新以反映epascarello和trincot提到的案例
答案 2 :(得分:0)
如果你想使用类型数组,请拉出文件类型,然后使用indexOf查看它是否在数组中
var fType = actualHref.split("/").pop().split(".")[1]
var isImg = imageTypes.indexOf(fType)>-1;
如果最后有一个查询字符串,这会失败。最好使用.pathname
代替.href