开发者

Regex in javascript for enum datatype of mysql

I am working with php and mysql. I have one dropdown box where I am asking datatype of mysql field. Now, I want to put javascript validation for it. I am confused with the enum datatype. I am using regular expression /^[']{1}[^',^\\]+[']{1}$/. This is for one single value of enum values. It is working fine but issue is when I put single quote or backslash with backslash, it is valid but this regular expression shows it as invalid.

开发者_运维百科For eg, 'a'b' is invalid but 'a\'b' is valid.


^'([^',^\\](\\.)?)+'$

[^',^\\] matches anything but the characters ',^\. Following this, we match an optional \. which matches any escaped character (including \. and \\). We match one or more sequences of this pair, thus matching an \' anywhere in the string but not requiring it.

Note that in your regular expression, [']{1} means match one ' character. This is equivalent to the literal '.


This should work: /^'(\\.|[^',^\\]+)+'$/
But, I would add whitespace fore and aft for validation (that can be stripped off with a capture group) /^\s*('(\\.|[^',^\\]+)+')\s*$/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜