简体   繁体   English

Javascript正则表达式问题

[英]Javascript regular expression question

I'm using javascript regex to do the following: 我正在使用javascript正则表达式执行以下操作:

I have the html content of a page saved inside a string, and I want to match all URLs on the page. 我将页面的html内容保存在字符串中,并且我希望匹配页面上的所有URL。

For example, if the document contains-- 例如,如果文档包含-

<script src = "http://www.a.com">
<a href="http://www.b.com">
<a href= "http://www.c.com">
<a href ="http://www.d.com">

I want the match to be-- 我想比赛是 -

http://www.a.com
http://www.b.com
http://www.c.com
http://www.d.com

Any help would be appreciated, thanks! 任何帮助将不胜感激,谢谢!

John Gruber has an excellent regex for URLs over at his site, Daring Fireball: http://daringfireball.net/2010/07/improved_regex_for_matching_urls John Gruber在他的网站Daring Fireball上有一个很好的URL正则表达式: http : //daringfireball.net/2010/07/improved_regex_for_matching_urls

You can implement it like so: 您可以像这样实现它:

function regex(url) {
    var regex = /(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/
    return regex.test(url);
}
function isUrl(url) {
    var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(url);
}

It is a bit more generic, but you may modify it for your needs. 它有点通用,但是您可以根据需要进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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