Including a hyphen in a regex character bracket?

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

    A more generic way of matching hyphens is by using the character class for hyphens and dashes ("\p{Pd}" without quotes). If you are dealing with text from various cultures and sources, you might find that there are more types of hyphens out there, not just one character. You can add that inside the [] expression

提交回复
热议问题