您还将常规 javascript 正则表达式传递给PseudoUrl
构造函数。
你会想要一个形式的正则表达式^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');
您可以传递一个 base url likehttp://www.example.com/pages/
和一个关键字 like foo
to createKeywordUrlRegex
,它会为您生成上面提到的正则表达式。