replace question

Discussion in 'Javascript' started by Rauan Maemirov, May 10, 2008.

  1. Hi, all. I'm trying to replace <object ...><embed... /></object> with
    <img> tag and vice-verse. It's like implementation of media plugin of
    Tiny MCE editor. I tried to watch their code, but it's too complicated
    and has a lot of unnecessary code. And the main reason is i'm weak in
    javascript rewriting. :)

    I need just simple rewrite for e.g. this

    <object width="425" height="355"><param name="movie" value="URL"></
    param><param name="wmode" value="transparent"></param><embed src="URL"
    type="application/x-shockwave-flash" wmode="transparent" width="425"
    height="355"></embed></object>

    <img width="425" height="355" title="src:'URL'">.

    param tags can be omitted.

    Could anybody help me? Thanks in advance.
     
    Rauan Maemirov, May 10, 2008
    #1
    1. Advertising

  2. On May 10, 9:37 pm, Rauan Maemirov <> wrote:
    > Hi, all. I'm trying to replace <object ...><embed... /></object> with
    > <img> tag and vice-verse. It's like implementation of media plugin of
    > Tiny MCE editor. I tried to watch their code, but it's too complicated
    > and has a lot of unnecessary code. And the main reason is i'm weak in
    > javascript rewriting. :)
    >
    > I need just simple rewrite for e.g. this
    >
    > <object width="425" height="355"><param name="movie" value="URL"></
    > param><param name="wmode" value="transparent"></param><embed src="URL"
    > type="application/x-shockwave-flash" wmode="transparent" width="425"
    > height="355"></embed></object>
    >
    > <img width="425" height="355" title="src:'URL'">.
    >
    > param tags can be omitted.
    >
    > Could anybody help me? Thanks in advance.


    I tried to do it myself, but all I did is to replaced

    <object width="425" height="355"><param name="movie" value="URL"></
    param><param name="wmode" value="transparent"></param><embed src="URL"
    type="application/x-shockwave-flash" wmode="transparent" width="425"
    height="355"></embed></object>

    with

    <object width="425" height="355"><param name="movie" value="URL"></
    param><param name="wmode" value="transparent"></param><img src="URL"
    type="application/x-shockwave-flash" wmode="transparent" width="425"
    height="355"></embed></object>
     
    Rauan Maemirov, May 11, 2008
    #2
    1. Advertising

  3. Rauan Maemirov wrote:
    > I tried to do it myself, but all I did is to replaced
    >
    > <object width="425" height="355"><param name="movie" value="URL"></
    > param><param name="wmode" value="transparent"></param><embed src="URL"
    > type="application/x-shockwave-flash" wmode="transparent" width="425"
    > height="355"></embed></object>
    >
    > with
    >
    > <object width="425" height="355"><param name="movie" value="URL"></
    > param><param name="wmode" value="transparent"></param><img src="URL"
    > type="application/x-shockwave-flash" wmode="transparent" width="425"
    > height="355"></embed></object>


    1. My tests indicate that a Flash movie is not played by an `img' element,
    so replacing the object-embed element combination would be futile, even
    though the `embed' element is proprietary (and therefore not Valid).

    2. Using client-side scripting to correct markup is the wrong approach
    as it does not need to be available.

    3. If necessary, you should rewrite the editor instead.

    4. FWIW, in Eclipse I would have used the following parameters:

    Search for:
    (?s)<object\s+.*?<embed\s+.*?(src=".+?")[^>]*?\s+(width=.+?>)</embed></object>

    Replace with:
    <img $1 $2 alt="">

    (Don't forget the `alt' attribute, give it a descriptive value if
    possible/applicable!)

    BTW, QuickREx again came in handy in finding that out:
    <http://www.bastian-bergerhoff.com/eclipse/features/web/QuickREx/toc.html>

    5. With the exception of `(?s)', which can be worked around with
    `(?:.|[\r\n])' instead of `.', this should also work with
    String.prototype.replace() in JavaScript 1.5+ (Mozilla/5.0),
    JScript 5.5+ (IE 5.5+), ECMAScript Ed. 3+.

    6. Replacing content this way might require using `innerHTML', a
    proprietary property that should be avoided in favor of DOM 2 scripting.


    HTH

    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
     
    Thomas 'PointedEars' Lahn, May 11, 2008
    #3
  4. On May 12, 4:43 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > Rauan Maemirov wrote:
    > > I tried to do it myself, but all I did is to replaced

    >
    > > <object width="425" height="355"><param name="movie" value="URL"></
    > > param><param name="wmode" value="transparent"></param><embed src="URL"
    > > type="application/x-shockwave-flash" wmode="transparent" width="425"
    > > height="355"></embed></object>

    >
    > > with

    >
    > > <object width="425" height="355"><param name="movie" value="URL"></
    > > param><param name="wmode" value="transparent"></param><img src="URL"
    > > type="application/x-shockwave-flash" wmode="transparent" width="425"
    > > height="355"></embed></object>

    >
    > 1. My tests indicate that a Flash movie is not played by an `img' element,
    >    so replacing the object-embed element combination would be futile, even
    >    though the `embed' element is proprietary (and therefore not Valid)..
    >
    > 2. Using client-side scripting to correct markup is the wrong approach
    >    as it does not need to be available.
    >
    > 3. If necessary, you should rewrite the editor instead.
    >
    > 4. FWIW, in Eclipse I would have used the following parameters:
    >
    > Search for:
    > (?s)<object\s+.*?<embed\s+.*?(src=".+?")[^>]*?\s+(width=.+?>)</embed></object>
    >
    > Replace with:
    > <img $1 $2 alt="">
    >
    > (Don't forget the `alt' attribute, give it a descriptive value if
    > possible/applicable!)
    >
    > BTW, QuickREx again came in handy in finding that out:
    > <http://www.bastian-bergerhoff.com/eclipse/features/web/QuickREx/toc.html>
    >
    > 5. With the exception of `(?s)', which can be worked around with
    >    `(?:.|[\r\n])' instead of `.', this should also work with
    >    String.prototype.replace() in JavaScript 1.5+ (Mozilla/5.0),
    >    JScript 5.5+ (IE 5.5+), ECMAScript Ed. 3+.
    >
    > 6. Replacing content this way might require using `innerHTML', a
    >    proprietary property that should be avoided in favor of DOM 2 scripting.
    >
    > HTH
    >
    > PointedEars
    > --
    > Anyone who slaps a 'this page is best viewed with Browser X' label on
    > a Web page appears to be yearning for the bad old days, before the Web,
    > when you had very little chance of reading a document written on another
    > computer, another word processor, or another network. -- Tim Berners-Lee


    I tried your regex:


    var flash = '<object width="425" height="355"><param name="movie"
    value="http://www.youtube.com/v/yVjzd320gew&hl=en"></param><param
    name="wmode" value="transparent"></param><embed src="http://
    www.youtube.com/v/yVjzd320gew&hl=en" type="application/x-shockwave-
    flash" wmode="transparent" width="425" height="355"></embed></
    object>';

    flash = flash.replace('(?s)<object\s+.*?<embed\s+.*?(src=".+?")[^>]*?\s
    +(width=.+?>)</embed></object>', '<img $1 $2 alt=""> ');

    It returns the same object...
     
    Rauan Maemirov, May 12, 2008
    #4
  5. On May 12, 4:43 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > Rauan Maemirov wrote:
    > > I tried to do it myself, but all I did is to replaced

    >
    > > <object width="425" height="355"><param name="movie" value="URL"></
    > > param><param name="wmode" value="transparent"></param><embed src="URL"
    > > type="application/x-shockwave-flash" wmode="transparent" width="425"
    > > height="355"></embed></object>

    >
    > > with

    >
    > > <object width="425" height="355"><param name="movie" value="URL"></
    > > param><param name="wmode" value="transparent"></param><img src="URL"
    > > type="application/x-shockwave-flash" wmode="transparent" width="425"
    > > height="355"></embed></object>

    >
    > 1. My tests indicate that a Flash movie is not played by an `img' element,
    >    so replacing the object-embed element combination would be futile, even
    >    though the `embed' element is proprietary (and therefore not Valid)..
    >
    > 2. Using client-side scripting to correct markup is the wrong approach
    >    as it does not need to be available.
    >
    > 3. If necessary, you should rewrite the editor instead.
    >
    > 4. FWIW, in Eclipse I would have used the following parameters:
    >
    > Search for:
    > (?s)<object\s+.*?<embed\s+.*?(src=".+?")[^>]*?\s+(width=.+?>)</embed></object>
    >
    > Replace with:
    > <img $1 $2 alt="">
    >
    > (Don't forget the `alt' attribute, give it a descriptive value if
    > possible/applicable!)
    >
    > BTW, QuickREx again came in handy in finding that out:
    > <http://www.bastian-bergerhoff.com/eclipse/features/web/QuickREx/toc.html>
    >
    > 5. With the exception of `(?s)', which can be worked around with
    >    `(?:.|[\r\n])' instead of `.', this should also work with
    >    String.prototype.replace() in JavaScript 1.5+ (Mozilla/5.0),
    >    JScript 5.5+ (IE 5.5+), ECMAScript Ed. 3+.
    >
    > 6. Replacing content this way might require using `innerHTML', a
    >    proprietary property that should be avoided in favor of DOM 2 scripting.
    >
    > HTH
    >
    > PointedEars
    > --
    > Anyone who slaps a 'this page is best viewed with Browser X' label on
    > a Web page appears to be yearning for the bad old days, before the Web,
    > when you had very little chance of reading a document written on another
    > computer, another word processor, or another network. -- Tim Berners-Lee


    I tried to use QuickREx (application, that U noticed) and indeed, it
    show matches correct. But in javascript it doesn't replace my text at
    all.
     
    Rauan Maemirov, May 12, 2008
    #5
  6. [trimmed attribution novel]

    Rauan Maemirov wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> Rauan Maemirov wrote:
    >>> I tried to do it myself, but all I did is to replaced
    >>> <object width="425" height="355"><param name="movie" value="URL"></
    >>> param><param name="wmode" value="transparent"></param><embed src="URL"
    >>> type="application/x-shockwave-flash" wmode="transparent" width="425"
    >>> height="355"></embed></object>
    >>> with
    >>> <object width="425" height="355"><param name="movie" value="URL"></
    >>> param><param name="wmode" value="transparent"></param><img src="URL"
    >>> type="application/x-shockwave-flash" wmode="transparent" width="425"
    >>> height="355"></embed></object>

    >> [...]
    >> 4. FWIW, in Eclipse I would have used the following parameters:
    >>
    >> Search for:
    >> (?s)<object\s+.*?<embed\s+.*?(src=".+?")[^>]*?\s+(width=.+?>)</embed></object>
    >>
    >> Replace with:
    >> <img $1 $2 alt="">
    >>
    >> (Don't forget the `alt' attribute, give it a descriptive value if
    >> possible/applicable!)
    >>
    >> BTW, QuickREx again came in handy in finding that out:
    >> <http://www.bastian-bergerhoff.com/eclipse/features/web/QuickREx/toc.html>
    >>
    >> 5. With the exception of `(?s)', which can be worked around with
    >> `(?:.|[\r\n])' instead of `.', this should also work with
    >> String.prototype.replace() in JavaScript 1.5+ (Mozilla/5.0),
    >> JScript 5.5+ (IE 5.5+), ECMAScript Ed. 3+.
    >> [...]

    >
    > I tried your regex:
    >
    > var flash = '<object width="425" height="355"><param name="movie"
    > value="http://www.youtube.com/v/yVjzd320gew&hl=en"></param><param
    > name="wmode" value="transparent"></param><embed src="http://
    > www.youtube.com/v/yVjzd320gew&hl=en" type="application/x-shockwave-
    > flash" wmode="transparent" width="425" height="355"></embed></
    > object>';
    >
    > flash = flash.replace('(?s)<object\s+.*?<embed\s+.*?(src=".+?")[^>]*?\s
    > +(width=.+?>)</embed></object>', '<img $1 $2 alt=""> ');
    >
    > It returns the same object...


    It returns a primitive string value, not an object. It returns the same
    string it is being applied to because you have not passed a RegExp object
    but a string as argument, and you have not read my posting thoroughly
    enough. If you had quoted properly, you would have been forced to re-read
    before replying and had probably not made this silly mistake.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
     
    Thomas 'PointedEars' Lahn, May 12, 2008
    #6
  7. On May 12, 4:58 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > [trimmed attribution novel]
    >
    >
    >
    > Rauan Maemirov wrote:
    > > Thomas 'PointedEars' Lahn wrote:
    > >> Rauan Maemirov wrote:
    > >>> I tried to do it myself, but all I did is to replaced
    > >>> <object width="425" height="355"><param name="movie" value="URL"></
    > >>> param><param name="wmode" value="transparent"></param><embed src="URL"
    > >>> type="application/x-shockwave-flash" wmode="transparent" width="425"
    > >>> height="355"></embed></object>
    > >>> with
    > >>> <object width="425" height="355"><param name="movie" value="URL"></
    > >>> param><param name="wmode" value="transparent"></param><img src="URL"
    > >>> type="application/x-shockwave-flash" wmode="transparent" width="425"
    > >>> height="355"></embed></object>
    > >> [...]
    > >> 4. FWIW, in Eclipse I would have used the following parameters:

    >
    > >> Search for:
    > >> (?s)<object\s+.*?<embed\s+.*?(src=".+?")[^>]*?\s+(width=.+?>)</embed></object>

    >
    > >> Replace with:
    > >> <img $1 $2 alt="">

    >
    > >> (Don't forget the `alt' attribute, give it a descriptive value if
    > >> possible/applicable!)

    >
    > >> BTW, QuickREx again came in handy in finding that out:
    > >> <http://www.bastian-bergerhoff.com/eclipse/features/web/QuickREx/toc.html>

    >
    > >> 5. With the exception of `(?s)', which can be worked around with
    > >>    `(?:.|[\r\n])' instead of `.', this should also work with
    > >>    String.prototype.replace() in JavaScript 1.5+ (Mozilla/5.0),
    > >>    JScript 5.5+ (IE 5.5+), ECMAScript Ed. 3+.
    > >> [...]

    >
    > > I tried your regex:

    >
    > > var flash = '<object width="425" height="355"><param name="movie"
    > > value="http://www.youtube.com/v/yVjzd320gew&hl=en"></param><param
    > > name="wmode" value="transparent"></param><embed src="http://
    > >www.youtube.com/v/yVjzd320gew&hl=en" type="application/x-shockwave-
    > > flash" wmode="transparent" width="425" height="355"></embed></
    > > object>';

    >
    > > flash = flash.replace('(?s)<object\s+.*?<embed\s+.*?(src=".+?")[^>]*?\s
    > > +(width=.+?>)</embed></object>', '<img $1 $2 alt=""> ');

    >
    > > It returns the same object...

    >
    > It returns a primitive string value, not an object.  It returns the same
    > string it is being applied to because you have not passed a RegExp object
    > but a string as argument, and you have not read my posting thoroughly
    > enough.  If you had quoted properly, you would have been forced to re-read
    > before replying and had probably not made this silly mistake.
    >
    > PointedEars
    > --
    >     realism:    HTML 4.01 Strict
    >     evangelism: XHTML 1.0 Strict
    >     madness:    XHTML 1.1 as application/xhtml+xml
    >                                                     -- Bjoern Hoehrmann


    Thanks, Thomas. I found my mistake. But there is the next trouble.
    What if I have more than one <object>'s. It returns only one image. I
    need to construct regexp the way, that it will replace only one
    node(tag). E.g. <object>...<embed src="title".../></
    object>...<object>...<embed ... width="455".../></object>
     
    Rauan Maemirov, May 14, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    400
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    369
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,618
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    251
    Dr John Stockton
    Aug 27, 2004
  5. Replies:
    3
    Views:
    187
    Brian McCauley
    Sep 12, 2005
Loading...

Share This Page