vbscript实现正则替换参数为函数
vbscript如何使用正则替换,替换参数为函数,可以处理分组中的数据后再返回需要的数据。
javascript的正则替换被替换内容可以为参数,但是vbscript的正则替换没有这种功能,替换成的内容只能为字符串,不能为一个返回字符串的函数。
下面是一个老外写的vbscript实现的替换成的内容可以为一个回调函数博文,原文如下
来源:http://gotochriswest.com/blog/2011/07/18/vbscript-regexp-replace-using-a-callback-function/
One of the nice things about JavaScript is its functional nature. This is especially nice when it comes to dealing with strings and regular expressions. For instance, in JavaScript, you can use the following code to capitalize every other letter in a string:
var str = "where in the world is carmen sandiego?"; var strWeird = str.replace(/(.)(.)/g, function(a,b,c) { return b.toUpperCase() + c; }); alert(strWeird); // WhErE In tHe wOrLd iS CaRmEn sAnDiEgO?Cool stuff, right? Wouldn’t it be nice to be able to use a similar approach in VBScript? Believe it or not, you can? Here is the definition for a function which will allow you to do something similar:
Function RegExpReplace(re, str, replacement) ' If replacement is a string, use the native RegExp.Replace function. If TypeName(replacement) = "String" Then RegExpReplace = re.Replace(str, replacement) ' Since replacement is not a string, call replacement with every match ' object and replace the match with the return value. Else Dim mc, m, ret, offset offset = 0 Set mc = re.Execute(str) For Each m In mc ret = replacement(m) str = Left(str, m.FirstIndex - offset) & ret _ & Mid(str, m.FirstIndex + m.Length - offset + 1) offset = offset + m.Length - Len(ret) Next RegExpReplace = str End If End FunctionThe above function takes three parameters: the regular expression, the string that may be changed and the replacement function (or string). Now the question is, how do we pass the function (or at least a reference to it)? We can do this by taking the name of the function and using the
GetRef
function to get a reference to it. The following is the equivalent of what was done in JavaScript at the onset of this post:Function fnUp1(objMatch) fnUp1 = UCase(m.Submatches(0)) & m.Submatches(1) End Function Dim re: Set re = New RegExp re.Pattern = "(.)(.)" re.Global = True Dim str: str = "where in the world is carmen sandiego?"; Dim strWeird: strWeird = RegExpReplace(re, str, GetRef("fnUp1")) MsgBox strWeird ' WhErE In tHe wOrLd iS CaRmEn sAnDiEgO?Okay, of course the code is not as short in JavaScript, because of the way that regular expressions must be created and the fact that anonymous functions don’t exist in the language, but this is just a simple example. You may need to use this function in many different places in your code.
The other thing that I briefly mentioned is that the third parameter may be a string instead of a reference to a function. This is basically a shortcut for the RegExp.Replace function which natively exists.
Now you see that it is possible to script in a functional way with VBScript. Still, as is evidenced by the examples, JavaScript (and JScript) can usually accomplish the same thing with less code.
加支付宝好友偷能量挖...