Skip to content

Instantly share code, notes, and snippets.

@screeley
Created February 6, 2012 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save screeley/1751899 to your computer and use it in GitHub Desktop.
Save screeley/1751899 to your computer and use it in GitHub Desktop.
Simple replace text url with video
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://scripts.embed.ly/jquery.embedly-2.1.7.min.js"></script>
</head>
<body>
<p>
This is a generic piece of text that contains a http://www.youtube.com/watch?v=t3TmFyyB4zI url.
</p>
<script>
// url Regex
var urlRe = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
$('p').each(function(i,e){
// Extract all of the urls out of the text.
var urls = $(this).text().match(urlRe);
// Call Embedly
$.embedly(urls, {}, function(o,d){
// Replace url in the text with html content
var h = $(e).text().replace(d.url, o.html);
// Set html content
$(e).html(h);
})
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment