Javascript determines whether a string is an image path

javascript determines whether a string is an image path?

I have an uncertain string: it could be: http://www.baidu.com (for url address path)
or it could be: https://img.codeshelper.com/upload/img/2021/02/28/pk4nm4innyg1209.png (for picture)

how can I tell if this string is a picture?

Feb.28,2021

think of webpack's feelings about the packaging of pictures.
regular


const PICTURE_EXPRESSION = /\.(png|jpe?g|gif|svg)(\?.*)?$/
const picReg = new RegExp (PICTURE_EXPRESSION)
url
const URL_REGULAR_EXPRESSION = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/
const objExp = new RegExp(URL_REGULAR_EXPRESSION)
~

alas, directly intercept the format to judge

var url =' http://www.baidu.com';
var sub = url.substring ('.));
if (sub = '.com' | | sub = '.cn') {
console.log ('Yes, this is the URL')
} else {
console.log ('Yes, this is a picture')
}


doesn't seem to be a picture path, but it can also be a picture. For a URL, a picture is just an output. It has nothing to do with the path


wrote a rule, which should be good:

function check_is_img(url) {
  return (url.match(/\.(jpeg|jpg|gif|png)$/) != null)
}



url.split('.')[1]=='png'||url.split('.')[1]=='jpg'

add other pictures yourself

Menu