SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reg exp and replace

    Hey,

    I have this

    Code JavaScript:
    var id = 'cart.php?id=12345&ref=home';
    var idPattern = /\?id=[0-9]/i;
    var tempID = id.match(idPattern);
     
    if (tempID != null) {
      var cartID = tempID.replace('?id=', '');
      alert(cartID);
    }

    tempID stores ?id=12345

    When I try to remove the ?id= with replace it gives me an error

    "tempID.replace is not a function"

    Does anyone know where I'm going wrong? And How I can fix this. thanks.

  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)
    Normally you don't need to worry about data types in javascript but from time to time you do.
    replace is a function of the String object - and the type returned from the match function above is making it an integer.
    Code:
    String(tempID).replace('?id=', '');

  3. #3
    Non-Member
    Join Date
    Oct 2008
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah gotcha. thanks.

  4. #4
    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)
    No problemo.


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
  •