Replace youtube embed iframe by video ID - Javascript
I need to replace some youtube iframes by something like a bbcode. Example:
Sample content:
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum
[iframe width="560" height="315" src="http://www.youtube.com/embed/RbJjC98ArCI" frameborder="0" allowfullscreen][/iframe]
lorem ipsum l开发者_如何转开发orem ipsum lorem ipsum lorem ipsum
- note that I needed to replace the characteres < AND > BY [ AND ] or the iframe code wouldnt be shown.
The content replaced:
lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum
[youtube]RbJjC98ArCI[/youtube]
lorem ipsum lorem ipsum lorem ipsum lorem ipsum
Javascript sample code:
var content_with_bbcodes=content.replace(SOME_REGULAR_EXPRESSION_HERE, '[youtube]$1[/youtube]');
I would also like the inverse. A regular expression to convert the bbcode to iframe...
Someone here experienced about regular expressions?
Here's something that should work.
var content_with_bbcodes=content.replace(/\[.+?embed\/(.+?)\".+\]/, '[youtube]$1[/youtube]');
As for the inverse, it's doable but you'll have to add the width and the height.
// sample
var height = 100;
var width = 200;
var content_iframe=content_with_bbcodes.replace(/\[youtube\](.+?)\[\/youtube\]/,'[iframe height="'+height+'" width="'+width+'" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen][/iframe]');
精彩评论