Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. var metaRegex = new RegExp(/<link.*?href=['"](.*?)['"].*?rel=['"]canonical['"].*?>/i);
  2. // return correctly: https://support.google.com/recaptcha/?hl=en
  3. // var metaRegex = new RegExp(/<link(?=.*rel=['"]canonical['"])(?=.*href=['"](.*?)['"]).*?>/i);
  4. // return incorrectly: https://www.google.com/accounts/TOS
  5. var metaTag = metaRegex.exec(body);
  6. console.log(metaTag[1]);
  7.  
  8. <link href="https://support.google.com/recaptcha/?hl=en" rel="canonical">
  9.  
  10. <link rel="canonical" href="https://support.google.com/recaptcha/?hl=en">
  11.  
  12. var body = '<link rel="stylesheet" href="my.css"/> <link href="https://support.google.com/recaptcha/?hl=en" rel="canonical"/> <a href="https://www.google.com/accounts/TOS"/>'
  13. var linkRegexp = /(<link[^>]*rel=['"]canonical['"][^>]*>)/;
  14. var hrefRegexp = /href=['"](.*?)['"]/;
  15.  
  16. var linkBody = linkRegexp.exec(body)[1];
  17. console.log(hrefRegexp.exec(linkBody)[1]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement