开发者

Regular expression for detecting hyperlinks

I've got this regex pattern from WMD showdown.js file.

/<((https?|ftp|dict):[^'">\s]+)>/gi

and the code is:

text = text.replace(/<((https?|ftp|dict):[^'">\s]+)>/gi,"<a href=\"$1\">$1</a>");

But when I set text to http://www.google.com, it does not anchor it, it returns the original text value as is (http:开发者_JS百科//www.google.com).

P.S: I've tested it with RegexPal and it does not match.


Your code is searching for a url wrapped in <> like: <http://www.google.com>: RegexPal.

Just change it to /((https?|ftp|dict):[^'">\s]+)/gi if you don't want it to search for the <>: RegexPal


As long as you know your url's start with http:// or https:// or whatever you can use:

/((https?|s?ftp|dict|www)(://)?)[A-Za-z0-9.\-]+)/gi

The expression will match till it encounters a character not allowed in the URL i.e. is not A-Za-z\.\-. It will not however detect anything of the form google.com or anything that comes after the domain name like parameters or sub directory paths etc. If that is your requirement that you can simply choose to terminate the terminating condition as you have above in your regex.

I know it seems pointless but it may be useful if you want the display name to be something abbreviated rather than the whole url in case of complex urls.


You could use:

var re = /(http|https|ftp|dict)(:\/\/\S+?)(\.?\s|\.?$)/gi;

with:

 el.innerHTML = el.innerHTML.replace(re, '<a href=\'$1$2\'>$1$2<\/a>$3');

to also match URLs at the end of sentences.

But you need to be very careful with this technique, make sure the content of the element is more or less plain text and not complex markup. Regular expressions are not meant for, nor are they good at, processing or parsing HTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜