SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    CA
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    type conversion of a backreference

    i'm having troubles doing a type conversion on a backreference from the string.replace method.

    here's a simple script to illustrate my problem:
    Code:
    string = 'it is 45 degrees outside today';
    
    temp = string.replace(/(\d+)/, parseInt("$1") * 2);
    
    alert(temp);
     //temp = 'it is NaN degrees outside today';
    is there any way i can convert the backreference into an integer to do some math calculations on it?

    thanks

  2. #2
    SitePoint Addict Guimauve's Avatar
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you can do this simply because the backreference isn't actually returned to you in any way, both parameters of the replace method are inputs and all of the processing takes place within the RegEx engine.

    I'm afraid you are going to have to extract the number, do the multiplication and THEN do a replace using the result.

    Charles


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
  •