Checking if a string contains a image

Would it be possible to detect if a string has an image in it. For example,
‘This is an image,’.
Then put this image into an

<img src={stringsource} />

And also keep the previous text that was there.
For example, i tried doing something like

const msgstring = 'Hello how are you, check out this image'
msgstring.replace(msgstring.slice(msgstring.indexOf('http'), msgstring.indexOf('png')+3),`<img src=${msgstring.slice(msgstring.indexOf('http'), msgstring.indexOf('png')+3)}>`)

But this is a static and not very good solution because it only works for images ending in png and they must start in http, or else the source will be invalid.

18 thoughts on “Checking if a string contains a image”

Leave a Comment