【发布时间】:2021-01-26 15:34:53
【问题描述】:
给定一串 html,如下所示:
<html>
<link rel="stylesheet" href="/thing.css">
<body>
<script src="/nothing.js"></script>
<link rel="stylesheet" href="/styles.css">
<a href='#a_hash'>A link</a>
</body>
</html>
我希望能够得到以下内容:
<html>
<link rel="stylesheet" href="//example.com/thing.css">
<body>
<script src="//example.com/nothing.js"></script>
<link rel="stylesheet" href="//example.com/styles.css">
<a href='//example.com#a_hash'>A link</a>
</body>
</html>
我最好在没有库的情况下使用原生 JavaScript 来执行此操作。 目前我有这个正则表达式来查找网址(我对新网址持开放态度!):
<.+?(?:href|src)=(?:"|')([^"']+)(?:"|').*?>
【问题讨论】:
-
将字符串转换为实际的 HTML,然后使用 DOM?
标签: javascript html regex replace