Created
July 11, 2012 01:31
-
-
Save josephwegner/3087360 to your computer and use it in GitHub Desktop.
Change img urls to point to CDN
This file contains 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
var html = "<body><div id='test'><script type='text/javascript' src='http://testScript.com'></script><img src='http://www.google.com' /><img class='test1' src='http://www.test.com' /><img src='http://woot.com' class='test2' /></div></body>"; | |
html.replace(/<img [^>]*src=["']([A-Za-z0-9\%\?\=\&\:\/\\\.]+)["']/gi, function(full,match) { | |
return full.replace(match, "http://my.cdn.com?url=" + escape(match)) | |
}); | |
//Outputs: | |
//"<body><div id='test'><script type='text/javascript' src='http://testScript.com'></script><img src='http://my.cdn.com?url=http%3A//www.google.com' /><img class='test1' src='http://my.cdn.com?url=http%3A//www.test.com' /><img src='http://my.cdn.com?url=http%3A//woot.com' class='test2' /></div></body>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment