84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
Multiple img tags, the src of each tag is different,
Now we need to do different processing for images whose src is base64 encoded and non-base64 images,
How should javascript distinguish whether the image is base64?
BASE64 codes always start with the formdata:image/xxx;base64,xxxxxx..., so just write a regular expression and test src
data:image/xxx;base64,xxxxxx...
$('img').each((i,item)=>{ let src = item.src if(src.indexOf('data:image/jpg;base64,')>-1){ // base64 图片操作 }else{ //path 图片操作 } })
Are non-base64 images all URL addresses?
Just match according to the beginning of src
$('img').each((i,item)=>{ let src = item.src if(src.indexOf('data:image')>-1){ // base64 图片操作 }else{ //path 图片操作 } })
You need to usestartWith, which is more efficient:
$('img').each((i,item)=>{ let src = item.src if(src.startWith('data:image')){ // base64 图片操作 }else{ //path 图片操作 } })
function validDataUrl(s) { return validDataUrl.regex.test(s); } validDataUrl.regex = /^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i; module.exports = validDataUrl;
BASE64 codes always start with the form
data:image/xxx;base64,xxxxxx...
, so just write a regular expression and test srcAre non-base64 images all URL addresses?
Just match according to the beginning of src
You need to usestartWith, which is more efficient: