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

How to get file suffix with jquery

藏色散人
藏色散人Original
2020-11-23 11:01:063621browse

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()) : '';

 

  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"){ 
                
     }

 

获取文件名和后缀:

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

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

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

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

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

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

 去掉后缀:

Js代码  

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

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!

Statement:
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