2

Currently i have this following regex which i use to validate the name of a company/industry and its working fine

  /(?=[a-zA-Z0-9-]{5,25}$)^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$/

The above regex doesnt supports for special characters like & - . _ which are valid in my case

I came up with this but it wasnt working as expected.

 /(?=[a-zA-Z0-9-\&\_\.]{5,25}$)^[a-zA-Z0-9\&\_\.]+(-[a-zA-Z0-9\&\_\.]+)*$/

Can someone point it out where my above regex goes wrong. Also a short explaination of the above regex wud be greatly appreciated Thanks

4

2 回答 2

3

我不认为你必须逃脱&\&同样的_方式

/(?=[a-zA-Z0-9-&_\.]{5,25}$)^[a-zA-Z0-9&_\.]+(-[a-zA-Z0-9&_\.]+)*$/
于 2013-04-25T08:38:27.007 回答
0

如果我没记错的话,你实际上不必在每个特殊字符上加上反斜杠,除非特殊字符是反斜杠本身或 character -。所以你的正则表达式是

/(?=[a-zA-Z0-9-&_.]{5,25}$)^[a-zA-Z0-9&_.]+(-[a-zA-Z0-9&_.]+)*$/
于 2013-04-25T08:40:53.993 回答