大家好我只想在a-z,A-Z,0-9, - ,&等表单字段中允许用户使用某些字符。和_。
我在正则表达式上很穷。使用jquery
检查字符串是否包含除这些字符以外的正则表达式函数是什么答案 0 :(得分:3)
答案 1 :(得分:2)
您可以将\w
用于字母,数字和下划线:
^[\w&-]+$
答案 2 :(得分:1)
就是这个
^[A-Za-z0-9\-_&]+$
^ Start of string
Char class [A-Za-z0-9\-\_] 1 to infinite times [greedy] matches:
A-Z A character range between Literal A and Literal Z
a-z A character range between Literal a and Literal z
0-9 A character range between Literal 0 and Literal 9
\-_& One of the following characters -_&
$ End of string
甚至是^[\w\d\-_&]+$
^ Start of string
Char class [\w\d\-\_] 1 to infinite times [greedy] matches:
\w Word character [a-zA-Z_\d]
\d Digit [0-9]
\-_& One of the following characters -_&
$ End of string
答案 3 :(得分:0)
你可以这样做:
function isValid(str) {
return (/^[a-zA-Z0-9_\-&]+$/gi).test(str);
}
并测试它:
console.log(isValid('aA0_-&')); // true
console.log(isValid('test*')); // false