Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6355657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:48:36+00:00 2026-05-24T22:48:36+00:00

I have a WYSIWYG textarea, and sometimes user’s may enter a youtube url into

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:48:37+00:00Added an answer on May 24, 2026 at 10:48 pm

    This regex will pick up the URLs and replace them with the embed markup (just an iframe according to what YouTube currently outputs).

    str.replace(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g, '<iframe width="420" height="345" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>');
    

    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…

    $('body').contents().each(function() {
    
        // Skip non text nodes.
        if (this.nodeType !== 3) {
            return true;
        }
    
        // Grab text
        var matches = this.data.match(/(?:http:\/\/)?(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch\?v=)?(.+)/g);
    
        if (!matches) {
            return true;
        }
    
        var iframe = $('<iframe width="420" height="345" frameborder="0" allowfullscreen />', {
            src: 'http://www.youtube.com/embed/' + matches[1]
        });
    
        iframe.insertAfter(this);
    
        $(this).remove();
    
    });
    

    Note that this inserts after the entire text node.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My clients can enter text into textarea and have the option to use the
I currently have a wysiwyg iframe where the user can submit input to another
I have a question about implementing WYSIWYG editors into CakePHP. I'm developing an intranet
Is there a way to add TinyMCE into my own WordPress plugin? I have
What options do I have to draw on a textarea? I know some wysiwyg
I have a wysiwyg editor ckeditor . It has a textarea. Okay, everything works
I have textarea and i want, when i post the form if there are
I have a textarea that I want to add a simple wysiwyg editor. I
I have a textarea setup with CKEditor (a WYSIWIG) in which the user is
I have some data (which users input using WYSIWYG editor). I have created a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.