大家好,我喜欢只允许用户在 az、AZ、0-9、-、& 和 _ 等表单字段中使用某些字符。
我不擅长正则表达式。使用jquery检查字符串是否包含这些字符以外的正则表达式函数是什么
您可以将\w
字母、数字和下划线用作:
^[\w&-]+$
只是这个
^[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
你可以这样做:
function isValid(str) {
return (/^[a-zA-Z0-9_\-&]+$/gi).test(str);
}
并像这样测试它:
console.log(isValid('aA0_-&')); // true
console.log(isValid('test*')); // false