SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extracting a certain section from text

    Hi,

    I have the following code and I want to extract the URL from it.

    Code:
    <iframe width="560" height="315" src="http://www.youtube.com/embed/-cZ4mdrlWYM" frameborder="0" allowfullscreen></iframe>
    I checked some jquery regular expression examples but couldn't connect the dots. Do you have any ideas on how to do that?

    Thanks.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    What about:

    Code JavaScript:
    var src = document.getElementsByTagName('iframe')[0].src;
    console.log(src);

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    What about:

    Code JavaScript:
    var src = document.getElementsByTagName('iframe')[0].src;
    console.log(src);
    Actually, it is not an element. That code will be pasted into a textarea and I want to filter it and display only the URL as the user pastes it.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Oh sorry.

    Try this:

    Code JavaScript:
    var str = '<iframe width="560" height="315" src="http://www.youtube.com/embed/-cZ4mdrlWYM" frameborder="0" allowfullscreen></iframe>';
    var src = str.match(/src="(.*?)"/);
    console.log(src[1]);

  5. #5
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It worked nice! Thank you very much Pullo.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •