JSFiddle - React, Tailwind, and code Playground
by niklasvh
HTML
<input type="button" id="button" value="Clicky" />
<textarea id="texty" style="width:400px;height:300px"> asd sad asdsa http://www.youtube.com/watch?v=u7TwqpWiY5s text</textarea>
JavaScript
$('#button').click(function() {
idPattern = /(?:(?:[^v]+)+v.)?([^&=]{11})(?=&|$)/;
// var vidId = prompt("YouTube Video", "Enter the id or url for your video");
var vidId;
el = document.getElementById('texty');
if (el.setSelectionRange) {
var vidId = el.value.substring(el.selectionStart,el.selectionEnd);
}
else if(document.selection.createRange()) {
var vidId = document.selection.createRange().text;
}
alert(vidId);
/*
Your regexp hangs up quite often, and it isn't a valid regexp either to check if its a valid Youtube url / video id.
var m = idPattern.exec(vidId);
if (m != null && m != 'undefined'){
//ed.execCommand('mceInsertContent', false, '[youtube id="'+m[1]+'"]');
alert('[youtube id="'+m[1]+'"]');
}
*/
});