在正则表达式字符括号中包含连字符? [英] Including a hyphen in a regex character bracket?

查看:143
本文介绍了在正则表达式字符括号中包含连字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$.validator.addMethod('AZ09_', function (value) { 
    return /^[a-zA-Z0-9.-_]+$/.test(value); 
}, 'Only letters, numbers, and _-. are allowed');

当我使用某些类似 test-123 它仍然会触发连字符无效。我试过 \ - -

When I use somehting like test-123 it still triggers as if the hyphen is invalid. I tried \- and --

推荐答案

使用 \ - 转义应该没问题,但您也可以尝试将其放在字符类的开头或结尾。这应该适合你:

Escaping using \- should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:

/^[a-zA-Z0-9._-]+$/

这篇关于在正则表达式字符括号中包含连字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆