我想匹配的只是一个URL的根,而不是一个文本字符串的整个URL。考虑到:
http://www.youtube.com/watch?v=ClkQA2Lb_iE
http://youtu.be/ClkQA2Lb_iE
http://www.example.com/12xy45
http://example.com/random
我想让最后2个实例解析到www.example.com或example.com域。
我听说正则表达式很慢,这将是我在页面上的第二个正则表达式,所以如果有办法做到没有正则表达式,请告诉我。
我正在寻找这个解决方案的JS/jQuery版本。
这个解决方案工作得很好,如果URL包含大量无效字符,您也可以使用。
安装PSL包
npm install --save psl
实现
const psl = require('psl');
const url= new URL('http://www.youtube.com/watch?v=ClkQA2Lb_iE').hostname;
const parsed = psl.parse(url);
console.log(解析)
输出:
{
input: 'www.youtube.com',
tld: 'com',
sld: 'youtube',
domain: 'youtube.com',
subdomain: 'www',
listed: true
}
这个解决方案工作得很好,如果URL包含大量无效字符,您也可以使用。
安装PSL包
npm install --save psl
实现
const psl = require('psl');
const url= new URL('http://www.youtube.com/watch?v=ClkQA2Lb_iE').hostname;
const parsed = psl.parse(url);
console.log(解析)
输出:
{
input: 'www.youtube.com',
tld: 'com',
sld: 'youtube',
domain: 'youtube.com',
subdomain: 'www',
listed: true
}