0

大家好,我喜欢只允许用户在 az、AZ、0-9、-、& 和 _ 等表单字段中使用某些字符。

我不擅长正则表达式。使用jquery检查字符串是否包含这些字符以外的正则表达式函数是什么

4

4 回答 4

3

尝试这个:

^[a-z0-9\-&_]+$/i

并在使用中:

/^[a-z0-9\-&_]+$/i.test(value); // = true|false

示例小提琴

于 2013-10-28T11:43:17.983 回答
2

您可以将\w字母、数字和下划线用作:

^[\w&-]+$
于 2013-10-28T11:46:48.417 回答
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
于 2013-10-28T11:44:16.447 回答
0

你可以这样做:

function isValid(str) {
    return (/^[a-zA-Z0-9_\-&]+$/gi).test(str);
}

并像这样测试它:

console.log(isValid('aA0_-&'));   // true
console.log(isValid('test*'));    // false
于 2013-10-28T11:46:22.340 回答