0

I'm doing a simple chat. When user sends message to server I want to parse this message. Whenever I find http://**** I want to convert it into a a tag, but all other characters need to be escaped, so that a user won't mess with html.

There are probably many ways to obtain it. Using some fancy regular expressions would be nice. Any ideas?

Note that I'm using Node.js.

freakish
  • 54,167
  • 9
  • 132
  • 169

1 Answers1

1

How about running each line of chat through this function before it is output to the user:

function replaceURLWithHTMLLinks(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"<a href='$1'>$1</a>"); 
}

Source: How to replace plain URLs with links?

Community
  • 1
  • 1
Gibron
  • 1,350
  • 1
  • 9
  • 28
  • Almost there. But at the same time I want to escape other chars. So for example: `test http://google.com ` will show everything with the link interchanged. – freakish Nov 09 '11 at 22:28
  • 1
    Oh, I just encode entire message at the begining and *THEN* use your parser. I think it works fine. Thank you! :) – freakish Nov 09 '11 at 22:31