如何用JavaScript正则表达式替换href中的所有内容
How can I replace everything inside an href with JavaScript regex?
我的文本类似于:
<a href="http://example.com/test this now">Stuff</a>
More stuff
<a href="http://example.com/more?stuff goes here">more</a>
我想用一个只对URL部分进行URL编码的函数来替换href
中的内容。
我该怎么做?
更新以下是我尝试过的:
postdata.comment.content = postdata.comment.content.replace(/href='"(.+?)'"/g, function(match, p1) {
return encodeURI(p1);
});
没有达到我的期望。
预期结果为:
<a href="http%3A%2F%2Fexample.com%2Ftest%20this%20now">Stuff</a>
More stuff
<a href="http%3A%2F%2Fexample.com%2Fmore%3Fstuff%20goes%20here">more</a>
正则表达式匹配完整的属性href="...."
,但是,替换仅由编码的URL完成,并使用encodeURIComponent()
对完整的URL进行编码。
var string = '<a href="http://example.com/test this now">Stuff</a>';
string = string.replace(/href="(.*?)"/, function(m, $1) {
return 'href="' + encodeURIComponent($1) + '"';
// ^^^^^^ ^
});
var str = `<a href="http://example.com/test this now">Stuff</a>
More stuff
<a href="http://example.com/more?stuff goes here">more</a>`;
str = str.replace(/href="(.*?)"/g, (m, $1) => 'href="' + encodeURIComponent($1) + '"');
console.log(str);
document.body.textContent = str;
对于编码,可以使用encodeURIComponent
:
var links = document.querySelectorAll('a');
for(var i=0; i<links.length; ++i)
links[i].href = encodeURIComponent(links[i].href);
<a href="http://example.com/test this now">Stuff</a>
More stuff
<a href="http://example.com/more?stuff goes here">more</a>
如果您只有一个HTML字符串而不是DOM元素,那么请使用不使用正则表达式。请改用DOM解析器来解析字符串。
var codeString = '<a href="http://example.com/test this now">Stuff</a>'nMore stuff'n<a href="http://example.com/more?stuff goes here">more</a>';
var doc = new DOMParser().parseFromString(codeString, "text/html");
var links = doc.querySelectorAll('a');
for(var i=0; i<links.length; ++i)
links[i].href = encodeURIComponent(links[i].href);
document.querySelector('code').textContent = doc.body.innerHTML;
<pre><code></code></pre>
请注意,如果您对URL进行完全编码,它将被视为相对URL。
免责声明:不要使用regex解析HTML
(原因太多,无法在此列出。)
但是,如果你坚持,这可能会奏效。
查找/(<['w:]+(?:[^>"']|"[^"]*"|'[^']*')*?'shref's*='s*)(?:(['"])(['S's]*?)'2)((?:"['S's]*?"|'['S's]*?'|[^>]*?)+>)/
替换$1$2
+someEncoding($3)+$2$4
扩展
( # (1 start)
< ['w:]+
(?: [^>"'] | " [^"]* " | ' [^']* ' )*?
's
href 's* = 's*
) # (1 end)
(?:
( ['"] ) # (2)
( # (3 start)
['S's]*?
) # (3 end)
'2
)
( # (4 start)
(?: " ['S's]*? " | ' ['S's]*? ' | [^>]*? )+
>
) # (4 end)
这是在哪里运行的?如果您有DOM,那么在document.links或document.querySelectorAll("a")上使用DOM循环比在HTML上使用regex要好得多。此外,您可能不想对所有内容进行编码,只想对搜索部分进行编码
var allLinks = document.querySelectorAll("a");
for (var i=0;i<allLinks.length;i++) {
var search = allLinks[i].search;
if (search) {
allLinks[i].search="?"+search.substring(1).replace(/stuff/,encodeURIComponent("something"));
}
}
如果你真的想有编码的hrefs,然后
for (var i=0;i<allLinks.length;i++) {
var href = allLinks[i].href;
if (href) {
allLinks[i].href=href.replace(/stuff/,encodeURIComponent("something"));
}
}
您期望的字符串"http%3A%2F%2Fexample.com%2Ftest%20this%20now"
对应于此操作encodeURIComponent("http://example.com/test this now")
,但不与encodeURI
函数对应:
var str = '<a href="http://example.com/test this now">Stuff</a>More stuff<a href="http://example.com/more?stuff goes here">more</a>';
str = str.replace(/href='"(.+?)'"/g, function (m, p1) {
return encodeURIComponent(p1);
});
console.log(str);
// <a http%3A%2F%2Fexample.com%2Ftest%20this%20now>Stuff</a>More stuff<a http%3A%2F%2Fexample.com%2Fmore%3Fstuff%20goes%20here>more</a>
- 在单击href链接的同时下载文件
- 如何用更合适的内容替换document.write
- 如果href包含X,请更改href
- 锚点元素的href属性自动更改
- 锚点元素不't使用svg时,请打开EDGE上的href
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- 如何用JavaScript正则表达式替换href中的所有内容
- 如何用jQuery替换文本区域中的href链接
- 如何用Javascript调用PHP页面;当用户单击href链接时进行jQuery
- 如何用不同的<a href="">
- 如何用小号将href属性刮入数组?
- 我如何用JavaScript设置或附加不同的base href到不同的链接
- 如何用梦魇用javascript函数点击a href
- 如何用href控制锚标记的切换
- 如何用Javascript为图表中的每一列添加更多的href元素
- 如何用Javascript替换鼠标悬停时的所有href元素
- 如何用javascript函数替换Url和href
- 如何用按钮替换html href链接
- 如何用javascript改变HTML href元素的一部分