2013-10-28 54 views

回答

3

試試這這些字符其他:

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

而且在使用中:

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

Example fiddle

+0

出於好奇:什麼是/我? – Johan

+1

不區分大小寫。 –

+0

/我是新來的,很酷:) – Rex

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 
+0

你不需要轉義'&'和'_' –

0

你可以這樣做:

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

並對其進行測試,如:

console.log(isValid('aA0_-&')); // true 
console.log(isValid('test*')); // false 
2

可以使用\w爲字母,數字和下劃線:

^[\w&-]+$ 
相關問題