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, talkwalker.com/images/2020/blog-headers/image-analysis.png’.
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 
https://www.talkwalker.com/images/2020/blog-headers/image-analysis.png'
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