Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.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
如何用JavaScript正则表达式替换href中的所有内容?_Javascript_Regex - Fatal编程技术网

如何用JavaScript正则表达式替换href中的所有内容?

如何用JavaScript正则表达式替换href中的所有内容?,javascript,regex,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> 没有做我希望的事 预期结果是: <a href="http%3A%2F%2Fexample.com%2Ftest%20this%20now">Stuff</a>

我的文本类似于:

<a href="http://example.com/test this now">Stuff</a>

More stuff

<a href="http://example.com/more?stuff goes here">more</a>
没有做我希望的事

预期结果是:

<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字符串=“”;
string=string.replace(/href=“(.*?”/),函数(m,$1){
return'href=“”+encodeURIComponent($1)+'”;
//      ^^^^^^                     ^
});
var str=`
更多的东西
`;
str=str.replace(/href=“(.*)”/g,(m,$1)=>'href=“”+encodeURIComponent($1)+'”);
console.log(str);

document.body.textContent=str对于编码,您可以使用
encodeURIComponent

var links=document.querySelectorAll('a');

对于(var i=0;i这在哪里运行?如果您有一个DOM,那么在document.links或document.querySelectorAll(“a”)上使用DOM循环比在HTML上使用正则表达式要好得多。 此外,您可能不希望对所有内容进行编码,只希望对搜索部分进行编码

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"));
   }
}
var allLinks=document.querySelectorAll(“a”);
对于(var i=0;i您期望的字符串
“http%3A%2F%2F example.com%2Ftest%20this%20now”
对应于此操作
encodeURIComponent(“http://example.com/test 这现在是“
,但不使用
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>
var str='More stuff';
str=str.replace(/href=\“(.+?)\”/g,函数(m,p1){
返回组件(p1);
});
console.log(str);
//填充更多填充更多
免责声明:不要使用正则表达式解析HTML
(此处列出的原因太多…)

但是,如果你坚持,这可能会奏效

查找
/(“].[^”]*“[^”]*”[^']*)*?\shref\s*=\s*)(?:(['”)([\s\s]*?)\2)((?:“[\s\s]*?”[\s\s]*?“[124;[^>]*)+>)/

替换
$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)
(#(1开始)
<[\w:]
(?: [^>"'] | " [^"]* " | ' [^']* ' )*?
\
href\s*=\s*
)#(一完)
(?:
( ['"] )                      # (2)
(#(3开始)
[\S\S]*?
)#(三完)
\2 
)
(#(4开始)
(?:“[\S\S]*?”|“[\S\S]*?”|[^>]*?)+
>
)#(四完)

您想修改这两个链接中的哪一个?预期的结果是什么?
$(pattern.attr('href','http://youpi.test/bidule');
No jQuery。我需要纯JS。在您访问它们之前,是在DOM中呈现锚定标记,还是您试图操纵内存中的字符串?我99%确定这是正确的方法,而不是您已经接受的方法。我可以用它来替换所有链接吗?@Shamoon是的,在regex上使用
g
标志将替换您在我创建了一个演示。从OP想要对整个URL进行编码的问题可以很清楚。是的,
http%3A%2F%2Fexample.com%2Ftest%20这个%20now
对于大多数事情来说可能毫无价值,但这正是OP想要的。他认为他想要的。无论如何,搜索“X/Y问题是什么”——更新为包含Hrefind而不是
new DOMParser().parseFromString(codeString,“text/html”);
,您可以执行
(function(){this.innerHTML=html;[…]).bind(document.createElement('div'))(html)
(可能快一点)@IsmaelMiguel是的,但前提是字符串是可信的。请尝试使用
'
非常好的方法。20票赞成!我不知道在DOM中没有添加任何元素时Javascript会运行。“这里列出的原因太多了”re̸gular exp之歌​这将是exti​nguish mor的声音​来自sp的tal man​在这里我能看到它。你能看到吗?我很漂亮​他最后一次否认了谎言​人的一切都是我的​他丢了乒乓球——是的,他来了,是的,他来了​或者渗透我的脸我的脸ᵒ上帝啊,不,不,不​别再这样了​*̶͑̾̾​第二部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分:第三部分​在本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本月,本本月,本月,本月,本月,本月,本本本月,本月,本本月,本本月,794​本校的一个月月内,本校的一个月月内的一个月内,该校的一个月内的一个月内的一个月内的一个月内的一个月内的一个月内的一个月内的一个月内的一个月内的本本本本本本本校的本本本本本校的本本本本本本月月月内的本本本本月月月月月的本本本本本本月月月月月的本本本本本本本月月月月月月月的本本本本本本月月月月的本本本本本月月月的本本本本月月月月月月月月的本本本本本本月月月的8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8日日日日日日,本本本本本本本本本本本本本本本本本月月月月月月月月月月的本本本本本本本本本本这是你需要的唯一原因
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>
 (                             # (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)