Regex Apify:PseudoUrl正则表达式,用于匹配包含给定关键字的URL
Apify支持JavaScript样式的正则表达式来匹配URL 我尝试按照正则表达式匹配包含特定关键字的所有URL-Regex Apify:PseudoUrl正则表达式,用于匹配包含给定关键字的URL,regex,apify,Regex,Apify,Apify支持JavaScript样式的正则表达式来匹配URL 我尝试按照正则表达式匹配包含特定关键字的所有URL- //not working http://www.example.com/[*foo*] 例如,如果一个网站有以下链接: http://www.example.com/pages/ http://www.example.com/pages/bar http://www.example.com/pages/foo/bar.html http://www.example.com/
//not working
http://www.example.com/[*foo*]
例如,如果一个网站有以下链接:
http://www.example.com/pages/
http://www.example.com/pages/bar
http://www.example.com/pages/foo/bar.html
http://www.example.com/pages/test-foo-test.html
http://www.example.com/pages/foo.html
正则表达式应该匹配最后3个URL。但是,正则表达式不起作用。您需要检查域填充之后的任何位置是否存在
foo
:
http:\/\/www\.example\.com\/.*foo
您需要检查域内容之后的任何位置是否存在
foo
:
http:\/\/www\.example\.com\/.*foo
您还将一个常规javascript正则表达式传递给
伪URL
构造函数
您需要一个格式为^http:\/\/www.example.com\/pages\/.*foo
的正则表达式
假设您希望对多个关键字执行此操作,您可以使用如下内容:
const Apify = require('apify');
const regexEscape = require('regex-escape');
function createKeywordUrlRegex(baseUrl, keyword) {
const regexStr = `^${regexEscape(baseUrl)}.*?${regexEscape(keyword)}`;
// remove the i if you want to match to be case-sensitive
return new RegExp(regexStr, 'i');
}
const purl = new Apify.PseudoUrl(createKeywordUrlRegex('http://www.example.com/pages/', 'foo'));
// print out the examples
const examples = [
'http://www.example.com/pages/',
'http://www.example.com/pages/bar',
'http://www.example.com/pages/foo/bar.html',
'http://www.example.com/pages/test-foo-test.html',
'http://www.example.com/pages/foo.html'
];
for(let example of examples)
console.log(example, purl.matches(example) ? 'MATCH!' : 'IGNORED');
您可以传递一个基本url,如
http://www.example.com/pages/
和一个关键字,如foo
到createKeywordUrlRegex
,它将为您生成上述正则表达式。您还将一个常规javascript正则表达式传递给伪URL
构造函数
您需要一个格式为^http:\/\/www.example.com\/pages\/.*foo
的正则表达式
假设您希望对多个关键字执行此操作,您可以使用如下内容:
const Apify = require('apify');
const regexEscape = require('regex-escape');
function createKeywordUrlRegex(baseUrl, keyword) {
const regexStr = `^${regexEscape(baseUrl)}.*?${regexEscape(keyword)}`;
// remove the i if you want to match to be case-sensitive
return new RegExp(regexStr, 'i');
}
const purl = new Apify.PseudoUrl(createKeywordUrlRegex('http://www.example.com/pages/', 'foo'));
// print out the examples
const examples = [
'http://www.example.com/pages/',
'http://www.example.com/pages/bar',
'http://www.example.com/pages/foo/bar.html',
'http://www.example.com/pages/test-foo-test.html',
'http://www.example.com/pages/foo.html'
];
for(let example of examples)
console.log(example, purl.matches(example) ? 'MATCH!' : 'IGNORED');
您可以传递一个基本url,如
http://www.example.com/pages/
和一个关键字,如foo
到createKeywordUrlRegex
,它将为您生成上述正则表达式。您也可以使用http:\/\/www\.example\.com\/.*foo
,如果没有lookahead,你也可以使用http:\/\/www\.example\.com\/.*foo
,如果没有lookahead,你真是太棒了!!!非常感谢你的回答。我在这个问题上浪费了太多时间…你太棒了!!!非常感谢你的回答。我在这个问题上浪费了太多时间。。