简体   繁体   中英

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#. 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. Can someone help me moving this function to 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...

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. 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. 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. 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? 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*$/);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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