Including a hyphen in a regex character bracket?

前端 未结 6 569
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 03:34
$.validator.addMethod(\'AZ09_\', function (value) { 
    return /^[a-zA-Z0-9.-_]+$/.test(value); 
}, \'Only letters, numbers, and _-. are allowed\');
6条回答
  •  萌比男神i
    2020-11-22 04:05

    The \- maybe wasn't working because you passed the whole stuff from the server with a string. If that's the case, you should at first escape the \ so the server side program can handle it too.

    • In a server side string: \\-
    • On the client side: \-
    • In regex (covers): -

    Or you can simply put at the and of the [] brackets.

提交回复
热议问题