Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从文本中检索相对URL_Node.js_Regex_Geturl - Fatal编程技术网

Node.js 从文本中检索相对URL

Node.js 从文本中检索相对URL,node.js,regex,geturl,Node.js,Regex,Geturl,我有一个包含绝对URL和相对URL的HTML字符串,我试图只检索相对URL。我尝试使用获取URL包,但这只检索绝对URL 接收到的html字符串的示例 <!DOCTYPE> <html> <head> <title>Our first HTML page</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> &l

我有一个包含绝对URL和相对URL的HTML字符串,我试图只检索相对URL。我尝试使用
获取URL
包,但这只检索绝对URL

接收到的html字符串的示例

<!DOCTYPE>
<html>
<head>

<title>Our first HTML page</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>
<body>

<h2>Welcome to the web site: this is a heading inside of the heading tags.</h2>

<p>This is a paragraph of text inside the paragraph HTML tags. We can just keep writing ...
</p>

<h3>Now we have an image:</h3>

<div><img src="/images/plantTracing.gif" alt="Graphic of a Mouse Pad"></div>

<h3>
This is another heading inside of another set of headings tags; this time the tag is an 'h3' instead of an 'h2' , that means it is a less important heading.
</h3>

<h4>Yet another heading - right after this we have an HTML list:</h4>

<ol>
<li><a href="https://github.com/">First item in the list</a></li>
<li><a href="/modules/example.md"> Second item in the list</a></li>
<li>Third item in the list</li>
</ol>

<p>You will notice in the above HTML list, the HTML automatically creates the numbers in the list.</p>

<h3>About the list tags</h3>
</body>
</html>

我们的第一个HTML页面
欢迎访问网站:这是标题标签中的标题。
这是段落HTML标记中的一段文本。我们可以继续写下去。。。

现在我们有一个图像: 这是另一组标题标签中的另一个标题;这一次标签是“h3”而不是“h2”,这意味着它是一个不太重要的标题。 还有一个标题-紧接着我们有一个HTML列表:
  • 清单中的第三项
  • 您会注意到,在上面的HTML列表中,HTML会自动创建列表中的数字

    关于列表标签
    目前正在这样做

    getURL(
    接收的HTML字符串

    它只返回{
    https://github.com/
    }


    我想返回{
    https://github.com/
    /modules/example.md
    }

    获取URL
    包要求URL要么以方案(如
    http://
    )开头,要么以已知的顶级域开头

    事实上,doc甚至包含这样一个要求URL有一个方案或引导www。将其视为URL。

    因为您要寻找的是两个路径都没有的相对路径,所以该包将不会执行您想要的操作


    您可能会从实际的HTML解析器中受益匪浅,例如,它可以根据HTML上下文查找基于HTML属性的URL,而不仅仅是文本匹配技巧,因为它可以查找所有相对URL的路径。

    在“我有一个文本”中,“文本”是什么意思。让我们看看你到底有什么。你有一个HTML字符串吗?您是否有从中获取HTML的URL?你是不是只有一段纯文本,却不知道格式是什么?你从什么开始?此外,这里关于代码的问题应该总是向我们展示您已经拥有的代码。请想一想在这里传达一个明确的问题需要什么。清晰的问题在这里得到快速的答案。不清楚的问题要么永远得不到答案,要么得不到支持票,要么被关闭。谢谢@jfriend00。我已经修改了我的问题。在我看来,获取URL包不是一个HTML解析器。我认为您可能需要一个HTML解析器。请记住,文本中的
    /images/plantTracing.gif
    本身不一定是URL。它也可以是一条路。要知道这是一个相对URL,必须了解需要解析HTML的上下文。有许多HTML解析器,例如,您可以从node.js.Note使用。
    get URL
    包在文档中包含以下说明:要求URL具有一个方案或引导www.被视为URL。因此,该包不会执行您想要的操作。@user2998991
    获取URL
    利用确定URL,并将根据TLD进行验证(在该场景中绝对路径不合格),因此您看到的结果是正确的。正如所建议的,您可以使用一个HTML解析器,如前面所建议的
    cheerio
    ,并手动提取HREF,这相当简单