Forum Moderators: open

Message Too Old, No Replies

RegExp issue with optionals

can't get an optional to work

         

mattglet

1:30 am on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the pattern:
"(^¦[ \t\r\n])((ftp¦http¦https¦gopher¦mailto¦news¦nntp¦telnet¦wais¦file¦prospero¦aim¦webcal): (([A-Za-z0-9$_.+!*(),;/?:@&~=-])¦%[A-Fa-f0-9]{2})+(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?)"

I can't seem to get the protocol to be optional. I'm trying to get both "http://www.example.com" and "www.example.com" to match (as it stands, a match only occurs with http: //, etc. present). I can't get the? in the correct spot, without messing the whole thing up. Optimally, I would like the colon and slashes to be optional too (i.e. http: //).Thanks for any help given.

I put spaces in the http so it wouldn't create a link on this board

mattglet

4:39 pm on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I stepped away from it, and put in some more work. I ended up simplifying it a little, and found a solution.

((^¦[ \t\r\n])(((http¦https¦ftp)\://)?)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s])

This pattern will match any url, with the protocol being optional. Combine it with some functionality that adds a link, and you've got an auto-activating url.