regex 更新HTML字符串链接

rsl1atfo  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(164)

如果没有https或http,则尝试更新href url

function urlify(text) {
var patt = /<a[^>]*href=["']([^"']*)["']/g;
    while(match=patt.exec(text)){
        if(match[1].match(/^([a-z0-9]+):\/\//)){
            console.log("skip");
        }else{
            return text.replace(match[1], function(url) {
                return 'https://' + url;
            })
        }
    }

}

var text = '<p>Find me at <a href="www.example.com">link1</a> and also at <a href="stackoverflow.com">link2</a> this is third link <a href="www.stackoverflow.com">link3</a</p>';

var html = urlify(text);
console.log(html);

只更新第一个url.需要到更新所有href的url

watbbzwu

watbbzwu1#

这是使用DOMParser.parseFromString()的解决方案
https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString
DOMParser接口的parseFromString()方法解析包含HTML或XML的字符串,返回HTMLDocument或XMLDocument。
比使用正则表达式解析HTML更可靠的解决方案:

function urlify(text) {
  const parser = new DOMParser();
  const d = parser.parseFromString(text, "text/html");
  d.querySelectorAll('a')
    .forEach(anchor => {   
      const href = anchor.getAttribute('href');  
      if ( !/^https?:\/\//.test(href) )
        anchor.setAttribute('href', `https://${href}`);
    })  
  return d.documentElement.querySelector('body').innerHTML;
}

var text = '<p>Find me at <a href="www.example.com">link1</a> and also at <a href="stackoverflow.com">link2</a> this is third link <a href="www.stackoverflow.com">link3</a</p>';

var html = urlify(text);
console.log(html);

相关问题