本文介绍了使用javascript Regex自动链接URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一段文字可能包含一些纯文本链接,或者是一些实际上是链接的链接。例如, 例如: 发布链接:http://test.com,发布图片< img src =http:// test.com/2.jpg/> ;.发布实际的A标记:< a href =http://test.com/test.html> http://test.com/test.html< / a> 我需要从这段文本中找出未格式化的链接。因此,任何正则表达式都会匹配第一种情况,但不会是第二种或第三种情况,因为它们已经格式化良好的链接。 我设法消除了所有链接到这个正则表达式:((http:| https:)\ / \ / [a-zA-Z0-9&#=。\ / \ - - ?_] +) code>,但是,仍然无法区分这两种情况。 这需要使用javascript,所以我认为不允许使用负面lookbehind。 任何帮助我都会感激。 编辑:在标签中捕获未格式化的链接。解决方案您可以使用此正则表达式来获取标签之外的URL: 请参阅演示 我们可以缩短它ab它也是一个 i 选项: (?![ ^<] * GT; | [^<。>] * LT; \ /)((HTTPS :) \ / \ / [A-Z0-9&安培;#= \ / \- ?_] +) 请参阅 div class =snippetdata-lang =jsdata-hide =false> var re = /(?![^<] *> | [^< Z0-9&安培;#= \ / \ - _] +)/ GI。?; var str ='发布链接:http://test.com,发布图片< img src =http://test.com/2.jpg/> ;.发布实际的A标记:< 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>'; innerHTML + =< br>< b>替换结果< / b> ;:+ result; < div id =res/> > 更新: b 要允许在特定标签内进行捕捉,您可以像这样将它们列入白名单: var re = /(?![^<] *> | [^<>< (?!(?: p |预)>))((HTTPS :) \ / \ / [A-Z0-9&安培;#= \ / \ - _。?] +?)/ GI; 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. 解决方案 You can use this regex to get URLs outside of tags:(?![^<]*>|[^<>]*<\/)((http:|https:)\/\/[a-zA-Z0-9&#=.\/\-?_]+)See demoWe can shorten it a bit, too, with an i option:(?![^<]*>|[^<>]*<\/)((https?:)\/\/[a-z0-9&#=.\/\-?_]+)See another demoSample 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"/>Update:To allow capturing inside specific tags, you can whitelist them like this:var re = /(?![^<]*>|[^<>]*<\/(?!(?:p|pre)>))((https?:)\/\/[a-z0-9&#=.\/\-?_]+)/gi; 这篇关于使用javascript Regex自动链接URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 11:37