简体   繁体   中英

Regex for finding URLs inside text

I want to find bulletproof RegEx for URLs inside text using JavaScript (I'm replacing urls with <a> tags), My current was RegEx:

var url_re = /https?:\/\/(?:(?!&[^;]+;)[^\s:"'<>)])+/g;

It was working but didn't match urls with port (like http://localhost:8080 ). Does this RegEx will match all urls or I miss something too?

var url_re = /https?:\/\/[^\/\s"'<>]+\/?(?:(?!&[^;]+;)[^\s:"'<>)])+/g;

Or maybe there is shorter/better regex for urls.

You can probably use this regex:

\bhttps?:\/\/(?:(?!&[^;]+;)[^\s"'<>)])+\b

Online Demo: http://regex101.com/r/wT2rG9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM