Created
November 12, 2019 11:51
-
-
Save darkmavis1980/5670af55ede680dd5c9cc184eab6bcfd to your computer and use it in GitHub Desktop.
Relative links to asbolute regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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