C#替换为回调函数就像在AS3就像、回调、函数

2023-09-08 12:30:40 作者:苟合

在AS3中,你有一个函数的字符串与此签名:

In AS3 you have a function on a string with this signature:

function replace(pattern:*, repl:Object):String

在REPL:对象也可以指定一个函数。如果指定一个函数,该函数返回的字符串插入替换匹配的内容。

The repl:Object can also specify a function. If you specify a function, the string returned by the function is inserted in place of the matching content.

此外,是有可能得到的,我要替换的东西原始字符串?

Also, is it possible to get the the original string in which I want to replace things?

(在AS3中,你可以通过

(In AS3 you can get the original string by

var input:String = arguments[2]; //in the callback function

我没有看到匹配类包含原始字符串的属性...

I don't see a property in the Match class containing the original string...

推荐答案

为了做到这一点在C#中,使用 System.Text.RegularEx pressions.Regex.Replace()这需要一个回调。

In order to do this in C#, use System.Text.RegularExpressions.Regex.Replace() which takes a callback.