What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » regexp problem

regexp problem
Author: Message:
agustin029
New Member
*

Avatar

Posts: 5
31 / Male / Flag
Joined: Feb 2012
O.P. regexp problem
how I can combine these two regex??

code:
link = msg.match(/((ftp|https|http)?:\/\/[^\s]+)/g); //returns http,https and ftp links
link2 = msg.match(/(www?.[^\s]+)/g); //returns www. links

09-21-2012 12:09 AM
Profile E-Mail PM Find Quote Report
whiz
Senior Member
****


Posts: 568
Reputation: 8
– / – / Flag
Joined: Nov 2008
RE: regexp problem
I think you might have a few issues with those expressions.  You need to escape literal "." characters (/./ matches any character).  Also, the ? after "www" only makes the third "w" optional (i.e. it will match "ww" or "www", which I'm assuming is not what you want).  Also, the former expression will match :// (without a protocol identifier; again, don't know if this is what you want or not).

But because you've used [^\s]+ to match the rest of the address, and the protocol/www is optional, this will match anything.

If you just want to match any URL, regardless of the protocol or www at the start, perhaps something like this?
js code:
link = msg.match(/(((ftp|https|http):\/\/)?[A-Za-z0-9-_~:/\?#@!\$&'\(\)\*\+,;=]+(\.[A-Za-z0-9-_~:\/\?#@!\$&'\(\)\*\+,;=]+)+)/g);

That looks a bit of a mouthful.  Basically, [A-Za-z0-9-_~:/\?#@!\$&'\(\)\*\+,;= ]+ matches any characters valid in a URL (which includes the www, so I've left that out.  It will try to match the protocol, followed by two or more sets of valid characters, separated by a ".".  So it would match foo.com, foo.co.uk, www.foo.co.uk, www.bar.foo.co.uk and so on, as well as any of these with a protocol at the start.

It's still not perfect though, as it will match any two (or more) sets of valid characters separated with a ".".
09-21-2012 09:34 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On