Javascript 正则表达式匹配符号:!$%^&*()_+|~-=`{}[]:";'<>?,./
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8359566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./
提问by pixelbobby
I'm trying to create a Regex test in JavaScript that will test a string to contain any of these characters:
我正在尝试在 JavaScript 中创建一个 Regex 测试,该测试将测试包含以下任何字符的字符串:
!$%^&*()_+|~-=`{}[]:";'<>?,./
More Info If You're Interested :)
更多信息,如果您有兴趣:)
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.
这是我正在开发的一个非常酷的密码更改应用程序。如果您有兴趣,这里是其余的代码。
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.
我有一个列出密码要求的表格,当最终用户输入新密码时,它将测试一组正则表达式并在相应的表格行中放置一个复选标记,如果它...签出:) 我只需要添加这个代替validation
数组中的第 4 项。
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])/).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);
}
Yes, there's also corresponding server-side validation!
是的,还有相应的服务器端验证!
回答by Jeff Hillman
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.
您还需要转义其他正则表达式元字符。
Edit: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: !"^_`[].
有三个范围。'$' 到 '/',':' 到 '?','{' 到 '~'。最后一串字符不能用范围更简单地表示:!"^_`[]。
Use an ACSII tableto find ranges for character classes.
使用ACSII 表查找字符类的范围。
回答by AmirZpr
The most simple and shortest way is to use this:
最简单和最短的方法是使用这个:
/[\W\S]/
It means: All characters that are not a digit or an English letter (\W
) or a white-space character (\S
).
它的意思是:所有不是数字或英文字母 ( \W
) 或空白字符 ( \S
) 的字符。
It maybe is not as perfect as Jeff's solution, but it's much simpler and I don't think it differs in practicality.
它可能不像杰夫的解决方案那么完美,但它更简单,我认为它在实用性上没有什么不同。
回答by MikeSchem
Answer
回答
/[^\w\s]/
Explanation
解释
This creates a negated character class removing the word characters and space characters. All that is left is the special characters.
这将创建一个否定字符类,删除单词字符和空格字符。剩下的就是特殊字符了。
\w
will select all "word" characters equivalent to [^a-zA-Z0-9_]
\s
will select all "whitespace" characters equivalent to [ \t\n\r\f\v]
\w
将选择等效于的所有“单词”字符[^a-zA-Z0-9_]
\s
将选择等效于的所有“空白”字符[ \t\n\r\f\v]
adding the ^
at the beginning of the class says to not select any of the following.
在^
课程开头添加表示不要选择以下任何一项。
回答by Arni Gudjonsson
// The string must contain at least one special character, escaping reserved RegEx characters to avoid conflict
const hasSpecial = password => {
const specialReg = new RegExp(
'^(?=.*[!@#$%^&*"\[\]\{\}<>/\(\)=\\\-_′+`~\:;,\.\|])',
);
return specialReg.test(password);
};
回答by Harfel Jaquez
A simple way to achieve this is the negative set [^\w\s]. This essentially catches:
实现此目的的一个简单方法是负集 [^\w\s]。这基本上抓住了:
- Anything that is not an alphanumeric character (letters and numbers)
- Anything that is not a space, tab, or line break (collectively referred to as whitespace)
- 任何非字母数字字符(字母和数字)
- 任何非空格、制表符或换行符(统称为空格)
For some reason [\W\S] does not work the same way, it doesn't do any filtering. A comment by Zael on one of the answers provides something of an explanation.
出于某种原因, [\W\S] 的工作方式不同,它不做任何过滤。Zael 对其中一个答案的评论提供了某种解释。
回答by Yaron Landau
Replace all latters from any language in 'A', and if you wish for example all digits to 0:
替换“A”中任何语言的所有后者,例如,如果您希望所有数字都为 0:
return str.replace(/[^\s!-@[-`{-~]/g, "A").replace(/\d/g, "0");