Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jquery - Auto-link URL with javascript Regex

I have a paragraph of text which may contain some links in plain text, or some links which are actually links.

For example:

Posting a link: http://test.com, posting an image <img src="http://test.com/2.jpg" />. Posting an actual A tag: <a href="http://test.com/test.html">http://test.com/test.html</a>

I need to fish out the unformatted links from this piece of text. So any regular expression that will match the first case, but not the second or third case because they are already well formatted links.

I've managed to fish out all the links with this regex: ((http:|https:)//[a-zA-Z0-9&#=./-?_]+), however, am still having trouble distinguishing between the cases.

This needs to be in javascript so I don't think negative lookbehind is allowed.

Any help would be appreciated.

EDIT: I'm trying to wrap the fished out unformatted links in an a tag.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use this regex to get URLs outside of tags:

(?![^<]*>|[^<>]*</)((http:|https:)//[a-zA-Z0-9&#=./-?_]+)

See demo

We can shorten it a bit, too, with an i option:

(?![^<]*>|[^<>]*</)((https?:)//[a-z0-9&#=./-?_]+)

See another demo

Sample code:

var re = /(?![^<]*>|[^<>]*</)((https?:)//[a-z0-9&#=./-?_]+)/gi; 
var str = 'Posting a link: http://test.com, posting an image <img src="http://test.com/2.jpg" />. Posting an actual A tag: <a href="http://test.com/test.html">http://test.com/test.html</a>';
var val = re.exec(str);
document.getElementById("res").innerHTML = "<b>URL Found</b>: " + val[1];
var subst = '<a href="$1">$1</a>'; 
var result = str.replace(re, subst);
document.getElementById("res").innerHTML += "<br><b>Replacement Result</b>: " + result;
<div id="res"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...