匹配符号的正则表达式:!$%^& *()_ + |〜 - =`{} []:&quot ;;'<>?,. / [英] Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./

查看:138
本文介绍了匹配符号的正则表达式:!$%^& *()_ + |〜 - =`{} []:&quot ;;'<>?,. /的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaScript中创建一个Regex测试,测试字符串以包含以下任何字符:

I'm trying to create a Regex test in JavaScript that will test a string to contain any of these characters:

!$%^&*()_+|~-=`{}[]:";'<>?,./

更多信息如果您有兴趣:)

这是我正在研究一个非常酷的密码更改应用程序。如果你感兴趣的是这里剩下的代码。

It's for a pretty cool password change application I'm working on. In case you're interested here's the rest of the code.

我有一个列出密码要求的表当最终用户键入新密码时,它将测试一个正则表达式数组并在相应的表行中放置一个复选标记,如果它...检出:)我只需要添加这一个来代替 validation array。

I have a table that lists password requirements and as end-users types the new password, it will test an array of Regexes and place a checkmark in the corresponding table row if it... checks out :) I just need to add this one in place of the 4th item in the validation array.

var validate = function(password){
    valid = true;

    var validation = [
        RegExp(/[a-z]/).test(password), RegExp(/[A-Z]/).test(password), RegExp(/\d/).test(password), 
        RegExp(/\W|_/).test(password), !RegExp(/\s/).test(password), !RegExp("12345678").test(password), 
        !RegExp($('#txtUsername').val()).test(password), !RegExp("cisco").test(password), 
        !RegExp(/([a-z]|[0-9])\1\1\1/).test(password), (password.length > 7)
    ]

    $.each(validation, function(i){
        if(this)
            $('.form table tr').eq(i+1).attr('class', 'check');
        else{
            $('.form table tr').eq(i+1).attr('class', '');
            valid = false
        }
    });

    return(valid);

}

是的,还有相应的服务器端验证!

推荐答案

这个正则表达式非常简单。只需使用一个字符类。连字符是字符类中的特殊字符,因此它必须是第一个:

The regular expression for this is really simple. Just use a character class. The hyphen is a special character in character classes, so it needs to be first:

/[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]/

您还需要转义其他正则表达式元字符。

You also need to escape the other regular expression metacharacters.

编辑:
连字符是特殊的,因为它可以用来表示一系列字符。这个相同的字符类可以通过以下范围进行简化:

The hyphen is special because it can be used to represent a range of characters. This same character class can be simplified with ranges to this:

/[$-/:-?{-~!"^_`\[\]]/

有三个范围。 '$'到'/',':'到'?','{'到'〜'。最后一个字符串不能用一个范围更简单地表示:!^ _` []。

There are three ranges. '$' to '/', ':' to '?', and '{' to '~'. the last string of characters can't be represented more simply with a range: !"^_`[].

使用ACSII表查找字符类的范围。

Use an ACSII table to find ranges for character classes.

这篇关于匹配符号的正则表达式:!$%^&amp; *()_ + |〜 - =`{} []:&quot ;;'&lt;&gt;?,. /的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆