About the regular expression of the picture!

problem description

for example, I have an array ["mai.jpg","da.png","xiao.gif"],
how can I write rules to match these images, so that pictures that are not in jpg,png,gif format cannot be uploaded!

the environmental background of the problems and what methods you have tried

function isImage (str) {
var reg = / w (.png | .jpg | .gif | .bmp | .psd | .tiff | .tga | .eps) / I;
return str.match (reg);
}
this seems to have no effect. Please let me know

.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

what result do you expect? What is the error message actually seen?

Dec.24,2021

/\. (png | jpe?g | gif | svg) (\. *)? $/

(. *?)\. (png | jpe?g | gif | bmp | psd | tiff | tga | eps)
matches any file name ending with these suffixes


if you are uploading a picture using the input control, you can do this

<input type="file" accept="image/gif, image/jpeg" />

if not, you can handle it this way

``var arr = ['1.jpghandwriting 2.pnghandwriting 3.txt'];

        arr.map(function(value,index){
            if(!/\.(png|jpg|gif|svg)(\?.*)?/.test(value)){
                arr.splice(index,1);
            };
        });
        console.log(arr)//['1.jpg','2.png']

there are actually two aspects to this problem

  1. if you can only select eligible files by qualifying extensions in input / file related components
  2. pre-read the file selected by file to determine whether it meets the criteria

for 1, there should be many examples, or your extension Filter, including

 <input type="file" name="pic" accept="image/gif,image.jpg" />
Things like

are similar methods. In fact, they only judged the extension of the file.

for 2, this generally requires a plug-in to implement (security mechanism)

Menu