-1

有人请帮忙。

我正在尝试编写一些正则表达式来在抓取网页时消除文件。我有像这样的网址

允许的 URL:
http://example.com/about/
http://example.com/test.html
http://example.com/about/careers.html

如果有任何页面不是像下面的 URL 那样的 html/xml 页面,我需要消除。

不允许/必需的 URL:
http://example.com/download/file_download.tar.gz
http://example.com/download/file.iso
http://example.com/download/something.dll

如何编写正则表达式来消除无效的 URL?我正在使用 javascript 来实现这一点。

我试过这样的东西。

URL = 'http://example.com';
filename = URL.substring(URL.lastIndexOf("/")+1, URL.length);
if(filename.match(/^#/g) || filename.match(/#$/g) || filename.match(/[^html]/g) || filename.match(/[^ ]/g)){    // filename contains id, 
4

2 回答 2

0

just try it this way /^(.*(?:\/|html|htm|php|xml))\s*$/

if (filename.match(/^(.*(?:\/|html|htm|php|xml))\s*$/)) {
    // yupp, it's ok
}
于 2013-11-06T07:19:24.707 回答
0

使用以下正则表达式过滤您的网址

(((http://)|(http://www.)|(www.))(example.com)([\/A-z]*)((.html)|(.xml))*)
于 2013-11-06T07:04:00.477 回答