SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how can get innerhtm value using regular expression?

    I.E.:

    ---------------------------
    Windows Internet Explorer
    ---------------------------
    <EMBED src=http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp; width=500 height=200 type=application/x-shockwave-flash allowfullscreen="true" allowscriptaccess="always"></EMBED>


    Pls give me an idea , how can i get "src" value in above code using regular expression?




    FF :


    <object width="500" height="200"><param name="movie" value="http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;"><param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always"><embed src="http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="200"></object>


    Pls give me an idea , how can i get "src" value in above code using regular expression?

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,122
    Mentioned
    29 Post(s)
    Tagged
    2 Thread(s)
    Hi samar,

    You don't need to use regular expression to get the value of an html attribute.
    the easiest way is to add an id to the element you are wanting to get the value of.
    <object id="flash"...>
    Code:
    document.getElementById('flash').value
    generally speaking dom is much easier to work with than regex.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markbrown4 View Post
    Hi samar,

    You don't need to use regular expression to get the value of an html attribute.
    the easiest way is to add an id to the element you are wanting to get the value of.
    <object id="flash"...>
    Code:
    document.getElementById('flash').value
    generally speaking dom is much easier to work with than regex.
    Hello , mark it is not working..Actualy we need answer with regx ...pls try with regx thnaks for quick reply

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Samar, you shouldn't use a regular expression. Since you're working on the client-side, you have an HTML parser at your disposal (the browser), why not use the available API to do what you're after (the DOM)?
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  5. #5
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well this works in IE and FF. Agree regex can be done, but seems to be unnecessary.

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <html>
    <head>
    <title>Find Src</title>
    <script>
    function init(){
    var emBeds = document.getElementsByTagName ('embed');
    var emBLen = emBeds.length;
    var srcElems = [];
     
    for (var i = 0; i<emBLen; i++){
    	srcElems[i] = emBeds[i].getAttribute('src');
    	alert (srcElems[i]);// pointless alert, but shows it works for both embeds.
    	}
    }
    window.onload = init;
    </script>
    </head>
     
    <body>
    <embed src='http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;' width=500 height=200 type=application/x-shockwave-flash allowfullscreen="true" allowscriptaccess="always"></embed>
    <object width="500" height="200">
    	<param name="movie" value="http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;"/>
    	<param name="allowFullScreen" value="true"/>
    	<param name="allowscriptaccess" value="always"/>
    	<embed src='http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;' type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="200"></embed>
    </object>
    </body>
     
    </html>

    Note: It just alerts the src addresses as a demonstration.

    edit: or using id names

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <html>
    <head>
    <title>Find Src</title>
    <script>
    function init(){
    var ieEmBed = document.getElementById ('ieEmbed').getAttribute('src');
    var ffEmBed = document.getElementById ('ffEmbed').getAttribute('src');
     
    alert (ieEmBed);
    alert (ffEmBed);
    }
    window.onload = init;
    </script>
    </head>
     
    <body>
    <embed id="ieEmbed" src='http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;' width=500 height=200 type=application/x-shockwave-flash allowfullscreen="true" allowscriptaccess="always"></embed>
    <object width="500" height="200">
    	<param name="movie" value="http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;"/>
    	<param name="allowFullScreen" value="true"/>
    	<param name="allowscriptaccess" value="always"/>
    	<embed id="ffEmbed" src='http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;' type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="200"></embed>
    </object>
    </body>
     
    </html>



    RLM

  6. #6
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Pointless for this problem, but a bit of practice with regular expressions.

    Something odd cropped up, which I didn't expect. document.getElementsByTagName('embed')[0].innerHTML draws a blank. Seems innerHTML and 'embed' don't play nicely, so wrapped the embeds in a div instead.

    The regular expression /src..([^\'\"]+)/;
    edit: Doesn't work in IE

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <html>
    <head>
    <title>Find Src</title>
    <script>
    function init(){
    var reg = /src..([^\'\"]+)/;
    var flash1Src = document.getElementById('flash1').innerHTML.match(reg);
    var flash2Src = document.getElementById('flash2').innerHTML.match(reg);
     
    alert(flash1Src[1]);
    alert(flash1Src[1]);
     
    }
    window.onload = init;
    </script>
    </head>
     
    <body>
    <div id="flash1">
    <embed src='http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;' width=500 height=200 type=application/x-shockwave-flash allowfullscreen="true" allowscriptaccess="always"></embed>
    </div>
    <div id="flash2">
    <object width="500" height="200">
    	<param name="movie" value="http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;"/>
    	<param name="allowFullScreen" value="true"/>
    	<param name="allowscriptaccess" value="always"/>
    	<embed src='http://www.youtube.com/v/JTmM3jut05Q&amp;hl=en&amp;fs=1&amp;' type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="200"></embed>
    </object>
    </div>
    </body>
     
    </html>

    RLM

  7. #7
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This one does.

    var reg = /src="?(.[^\s"]*)/;

    RLM

    ps. Like someone else said the other day. Would be nice to have the facility of editing previous posts.


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
  •