0

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>?

Chud37
  • 4,907
  • 13
  • 64
  • 116
  • Don't use regex for parsing HTML. Use the DOM to parse the HTML. https://stackoverflow.com/questions/10585029/parse-a-html-string-with-js –  Jul 17 '17 at 13:56
  • @Amy it's not so much as HTML as a URL, which is fine to parse. I just can't nail the regex. – Chud37 Jul 17 '17 at 13:57

0 Answers0