SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Regexp replace

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regexp replace

    Hi,

    I am really stuck with regexp replace.
    I want to replace a string after checking it is valid by passing the values to a function.

    The string is '<img width="300" height="300">'

    Code:
    var text = '<img width=300 height=300>';
    text = text.replace(/<img width\=(.*?) height\=(.*?)>/gi, check_img($1, $2));
    The $1 and $2 values are not being passed to the function.
    This same method works in PHP by the e Modifier but not in JS.

    Please Help.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In javascript, to execute on replace, the second parameter must be a function, e.g.

    Code:
    function check_img(tag, widht, height) {
        //........................
    }
    text = text.replace(/<img width\=(.*?) height\=(.*?)>/gi, check_img);

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But how to pass $1 and $2 to this function.
    In your example it is not passed to the function.

    EDIT:
    I get it now the $1...$9 are passed directly into the function parameters
    Thanks guys
    Last edited by helpmeinphp; Jul 13, 2007 at 08:11. Reason: Got that


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
  •