开发者

regex for alphanumeric with parentheses ()

开发者 https://www.devze.com 2023-04-13 03:20 出处:网络
i have a jQuery regex for alphanumeric of 6 to 255 length and i want to check for brackets \"()\" and other characters like underscore, comma, hyphen,

i have a jQuery regex for alphanumeric of 6 to 255 length and i want to check for brackets "()" and other characters like underscore, comma, hyphen,

/^[A-Za-z0-9,., ,_,-,\(\)]{6,255}$/

but there is something wrong with it in terms of brackets and it is also accepting script values which is not good in terms of security

As I explain better in a comment below, some strings that are ok:

ABCDEF
ABCDEFG
abcdef
0123456789
a.b.c.d.e
., _-()

Some strings that aren't ok

ABC
ABCDEF(
Abcdef(ghi
abcde)fgh

(the last two ones aren't ok becau开发者_StackOverflowse the brackets aren't matched)


/(?=^([^()]*\([^()]*\))*[^()]*$)^[A-Za-z0-9,. _()-]{6,255}$/;

You are checking too many times for , and the - must be the last character or be escaped (\-), otherwhise it's used as a range (A-Z)

I've added a precheck to test that all the ( have a ) and all the ) have a (. It won' work with (()) (two levels of brackets)

Test here: http://jsbin.com/epiroh/8/edit and http://gskinner.com/RegExr/?2uuab

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号