Skip to content

Instantly share code, notes, and snippets.

@darkmavis1980
Created November 12, 2019 11:51
Show Gist options
  • Save darkmavis1980/5670af55ede680dd5c9cc184eab6bcfd to your computer and use it in GitHub Desktop.
Save darkmavis1980/5670af55ede680dd5c9cc184eab6bcfd to your computer and use it in GitHub Desktop.
Relative links to asbolute regex
const text = `
<p>This is some test with <a href=\"/piesync.html\">a relative link</a> and one with an <a href=\"http://piesync.com\">absolute link</a></p>
<p>This is some test with <a href=\"/piesync.html\" title="test">a relative link</a> and one with an <a href=\"http://www.piesync.com\">absolute link</a></p>
<p>This is some test with <a href=\"/piesync/hello/path\">a relative link</a> and one with an <a href=\"http://piesync.com\">absolute link</a></p>
<p>This is some test with <a href=\"/\">a relative link</a> and one with an <a href=\"http://piesync.com\">absolute link</a></p>
<p>This is some test with <a href=\"/?_ga=2.12323232.131232131.423413123-1231231.123123123\">a relative link</a> and one with an <a href=\"http://piesync.com\">absolute link</a></p>
`;
let replaced = text.replace(/<a href="(\/([\w.\/?=-]+)|\/)/gm, `<a href="https://www.domain.com$1`);
console.log(replaced);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment