简体   繁体   English

有人可以帮我将这个用C#编写的正则表达式转换为javascript吗?

[英]Can someone help me to convert this regex expression written in C# to javascript?

I have to do some pattern matching for text in a textbox. 我必须在文本框中为文本做一些模式匹配。 I was doing it in postback event of server in C#. 我在C#中的服务器的回发事件中这样做。 My regex is as follows : 我的正则表达式如下:

 public bool ValidatePassword(string temp)
    {
        bool isMatch = false;
        passwd = passwd.Trim();

        isMatch = Regex.IsMatch(temp,
                             @"^           # Start of string
                        (?=.*\p{Lu})      # Assert at least one uppercase letter
                        (?=.*\p{Ll})      # Assert at least one lowercase letter
                        (?=.*\d)          # Assert at least one digit
                        (?=.*[^\p{L}\d])  # Assert at least one other character
                        .{8,13}           # Match at least 8 characters and maximum of 13 characters
                        $                 # End of string",
                             RegexOptions.IgnorePatternWhitespace);


        return isMatch;
    }

I want to move this to Javascript so that I do the matching on client side. 我想将它移动到Javascript,以便我在客户端进行匹配。 Can someone help me moving this function to Javascript? 有人可以帮助我将此功能移动到Javascript吗?

Something like: 就像是:

function ValidatePassword(temp) {
  return /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[^a-zA-Z\d]).{8,13}$/.test(temp);
}

JavaScript regular expression implementation understands only the case of Latin characters, it doesn't implement national characters semantics, so, unfortunately, there is no way to translate this regular expression, not even anywhere close to the origin... JavaScript正则表达式实现只理解拉丁字符的情况,它没有实现国家字符语义,所以,遗憾的是,没有办法翻译这个正则表达式,甚至没有任何接近原点的表达式...

If you are doing this as a learning exercise, then, perhaps, you could have a look at Perl implementation of regular expressions and have a copy of it. 如果您正在将此作为学习练习,那么,或许,您可以查看正则表达式的Perl实现并获得它的副本。 But if your question was more in the practical domain, then consider your target audience and what languages they might be typing in, look up Unicode codepoints for the minuscule / majuscule letters in their character sets and act accordingly. 但如果您的问题更多地出现在实际领域,那么请考虑您的目标受众以及他们可能输入的语言,查找Unicode代码点以查找其字符集中的小字母/大字母并相应地采取相应措施。 Also note that many non-Latin languages have no concept of the letter case. 另请注意,许多非拉丁语言都没有字母案例的概念。

Or, perhaps, consider stricter rules for passwords. 或者,也许,考虑更严格的密码规则。 :) :)

Matching upper- and lower-case letters is not that easy in Javascript, as it does not support \\p{…} character classes. 在Javascript中匹配大写和小写字母并不容易,因为它不支持\\p{…}字符类。 I suggest you to test this outside the regex. 我建议你在正则表达式之外进行测试。 Also, "other character" is hard to define, is that just [^a-z0-9] , or also no umlauts etc? 另外,“其他角色”很难定义,只是[^a-z0-9] ,还是没有变形金刚等? Btw, you rather should enforce longer passwords than more cryptic ones. 顺便说一句,你宁可应该使用比更神秘的密码更长的密码。

function validatePassword(temp) {
    // temp = temp.trim() - is not supported in all browsers or needs to be shimmed
    //                      will be done in the regex

    if (temp.toUpperCase() == temp) // no lowercase char
        return false;
    if (temp.toLowerCase() == temp) // no uppercase char
        return false;

    return temp.test(/^\s*(?=.*\d).{8,13}\s*$/);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM