Regular Expression Replace Question 
Author Message
 Regular Expression Replace Question

I'm trying to write a regex replace that works something like this:

some_string.replace(regex_stuff, do_something_to("$1"))

Where I want to pass the match ($1) to some function for manipulation
because what I want to do is too complicated to be done with a managable
regular expression.  Can I pass the match to a function like this?

Tim



Sat, 06 Sep 2003 05:25:06 GMT  
 Regular Expression Replace Question
Quoted directly from the JScript Language Reference:

The following example, which works in JScript 5.5 and later, performs a
Fahrenheit to Celsius conversion, illustrates using a function as
replaceText. To see how this function works, pass in a string containing a
number followed immediately by an "F" (e.g., "Water boils at 212").

function f2c(s) {
  var test = /(\d+(\.\d*)?)F\b/g;    //Initialize pattern.
  return(s.replace
    (test,
      function($0,$1,$2) {
        return((($1-32) * 5/9) + "C");
      }
    )
  );

Quote:
}

document.write(f2c("Water freezes at 32F and boils at 212F."));

JScript Documentation
http://msdn.microsoft.com/scripting/jscript/download/jsdoc.exe
http://msdn.microsoft.com/scripting/jscript/techinfo/jsdocs.htm

=-=-=
Steve
-=-=-


Quote:
> I'm trying to write a regex replace that works something like this:

> some_string.replace(regex_stuff, do_something_to("$1"))

> Where I want to pass the match ($1) to some function for manipulation
> because what I want to do is too complicated to be done with a managable
> regular expression.  Can I pass the match to a function like this?

> Tim



Sat, 06 Sep 2003 05:46:19 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Regular Expression replace question

2. Regular Expression replace question

3. regular expression href replace question

4. Regular Expressions with Replace question...

5. regular expression href replace question

6. regular expressions with string.replace

7. Regular expressions to replace characters outside HTML tags

8. regular expression replace

9. VBScript - Regular Expressions replace

10. Replace HTML Tag value? (Regular Expression)

11. regular expression and .replace help

12. Help, really simple question w/ Regular Expressions

 

 
Powered by phpBB® Forum Software