[removed] REGEX to change all relative Urls to Absolute

后端 未结 5 1895
灰色年华
灰色年华 2020-12-02 14:54

I\'m currently creating a Node.js webscraper/proxy, but I\'m having trouble parsing relative Urls found in the scripting part of the source, I figured REGEX would do the tri

5条回答
  •  醉梦人生
    2020-12-02 15:40

    From a comment by Rob W above about the base tag I wrote an injection function:

    function injectBase(html, base) {
      // Remove any  elements inside      
      html = html.replace(/(<[^>/]*head[^>]*>)[\s\S]*?(<[^>/]*base[^>]*>)[\s\S]*?(<[^>]*head[^>]*>)/img, "$1 $3");
    
      // Add  just before   
      html = html.replace(/(<[^>/]*head[^>]*>[\s\S]*?)(<[^>]*head[^>]*>)/img, "$1 " + base + " $2");  
      return(html);
    }
    

提交回复
热议问题