Home > Web Front-end > JS Tutorial > How to get file suffix with jquery

How to get file suffix with jquery

藏色散人
Release: 2020-11-30 16:38:03
Original
3696 people have browsed it

jquery获取文件后缀的方法:1、使用正则表达式“(/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()):'';”获取文件后缀;2、通过Js代码获取文件后缀。

How to get file suffix with jquery

推荐:《jquery视频教程

本教程操作环境:windows7系统、jquery3.2.1版本,该方法适用于所有品牌电脑。

jQuery 获取文件后缀的方法

方法一、

       采用正则表达式:

Js代码

var file=$("input[name='file']").val(); 
var filename=file.replace(/.*(\/|\\)/, ""); 
var fileExt=(/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : '';
Copy after login

filename得到文件名

fileExt得到后缀名

方法二、

Js代码

var location=$("input[name='file']").val(); 
     var point = location.lastIndexOf("."); 
       
     var type = location.substr(point); 
     if(type==".jpg"||type==".gif"||type==".JPG"||type==".GIF"){ 
                
     }
Copy after login

获取文件名和后缀:

1. 用split()来拆成数组 然后取下标0的。

     var arr = sFileName.split('.');
     alert(arr[0]);
Copy after login

2.使用一般后缀长度为4的特点:

    var name= sFileName.substr(0,sFileName.length-4)
Copy after login

3.获取文件名的正则表达式:

    var sFileName=sFile.replace(/.*(\/|\\)/, "");
Copy after login

去掉后缀:

Js代码

<script > 
$(document).ready(function(){ 
 var a=$("#image").attr("src") 
 alert(a.replace(".png","")) 
 }) 
</script> 
<img alt="aaa" id="image" src="theImage.png" />
Copy after login

The above is the detailed content of How to get file suffix with jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template