I have a WYSIWYG textarea, and sometimes user's may enter a youtube url into the box. On the server side, there are html filters to prevent "harmful" code from being saved.
So instead, I'd like to just keep the server code as-is, and run a jQuery document ready event that searches a block of text for a youtube link, and converts it to the iframe embed code.
I'd imagine it would be regex based, but I'm absolutely horrid with regex's (at some point, I really need to sit down and study them).
Two types of youtube links:
http://www.youtube.com/watch?v=t-ZRX8984sc
or
http://youtu.be/t-ZRX8984sc
This regex will pick up the URLs and replace them with the embed markup (just an
iframe
according to what YouTube currently outputs).jsFiddle.
However, this can mangle things such as event handlers attached with old school methods.
It is a bit more complicated, but the best way would be work with text nodes only.
That should look something like this...
Note that this inserts after the entire text node.
Ensure proper operation at the organization of WYSIWYG editor