I have a simply HTML/NodeJS chat that is working well. User sends a message to a room, the room receives it and displays it on all clients.
However I'd like the user to be a able (as you can everywhere today) paste a URL of a gif/image file and have it displayed in the chat.
Currently I match all hyperlinks with:
/(https?:\/\/([-\w\-\.]+)+(:\d+)?(\/([\w\-\/_\.]*(\?\S+)?)?)?)/ig
And on searching I found this answer which will detect image links with:
(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))
However I need one to replace hyperlinks and one to replace images and wrap them in <a>
and <img src=''
> respectively.
How can I acheive this>?