将一段js翻译成asp.net(涉及正则表达式)

时间:2015-10-30 09:49:33

标签: javascript c# asp.net regex vb.net

由于性能问题,我正在将一段编程代码从客户端移动到服务器端(注意:并非所有代码都在帖子中,只是我遇到问题的部分)

我遇到问题的具体部分是别名重写。有人可以帮我翻译这部分。

    //1. Force UperCamelCase: "Een lange financiele 443 naam" -> "Een Lange Financiele 443 Naam"
    var strTempAlias = this.strName.replace(/\b[a-z]/g, function (match) { return match.toUpperCase() });

    //2. Only allow a-zA-Z chars. "Een Lange Financiele 443 Naam" -> "EenLangeFinancieleNaam"
    strTempAlias = strTempAlias.replace(/[^a-zA-Z]/g, '');

    //3. Until max length 25: progressive remove small characters: "EenLangeFinaNaam"
    var intShrinkSize = 10
    while (strTempAlias.length > 25 && intShrinkSize > 3) {
        var r = new RegExp("([A-Z]+[a-z]{0," + intShrinkSize + "})([a-z]+)", "g")
        strTempAlias = strTempAlias.replace(r, function (match, $1, $2) { return $1 });
        intShrinkSize -= 2;
    }

我已经能够将步骤1和2转换为.net代码,但我无法弄清楚如何转换步骤3。

下面你可以在vb.net和c#中找到转换后的版本(对我而言,无论哪个版本都可以解答我都可以编程)

VB.NET

        '1. Force UperCamelCase: "Een lange financiele 443 naam" -> "Een Lange Financiele 443 Naam"
        Dim strTempAlias As String = StrConv(strAlias, VbStrConv.ProperCase)

        '2. Only allow a-zA-Z chars. "Een Lange Financiele 443 Naam" -> "EenLangeFinancieleNaam"
        strTempAlias = Regex.Replace(strTempAlias, "[^A-Za-z0-9]+", "")

        '3. Until max length 25: progressive remove small characters: "EenLangeFinaNaam"
        Dim shrinksize = 10
        While strTempAlias.Length > 25 AndAlso shrinksize > 3
            'last piece of code to translate
        End While

C#

//1. Force UperCamelCase: "Een lange financiele 443 naam" -> "Een Lange Financiele 443 Naam"
string strTempAlias = Strings.StrConv(strAlias, VbStrConv.ProperCase);

//2. Only allow a-zA-Z chars. "Een Lange Financiele 443 Naam" -> "EenLangeFinancieleNaam"
strTempAlias = Regex.Replace(strTempAlias, "[^A-Za-z0-9]+", "");

//3. Until max length 25: progressive remove small characters: "EenLangeFinaNaam"
dynamic shrinksize = 10;
while (strTempAlias.Length > 25 && shrinksize > 3) {
    //last piece of code to translate
}

}

1 个答案:

答案 0 :(得分:0)

int intShrinkSize = 10;
while (strTempAlias.Length > 25 && shrinksize > 3) {
   strTempAlias = Regex.Replace(strTempAlias, "([A-Z]+[a-z]{0," + intShrinkSize + "})([a-z]+)", "$1");
   intShrinkSize -= 2;
}