在正则表达式中允许-(破折号) [英] Allow - (dash) in regular expression

查看:683
本文介绍了在正则表达式中允许-(破折号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下正则表达式,但我希望文本框允许使用破折号

I have the following regular expression but I want the text box to allow the dash character

^[0-9a-zA-Z \/_?:.,\s]+$

有人知道我该怎么做吗?

Anyone know how I can do this?

推荐答案

破折号必须是字符类中的第一个/最后一个字符,才能按字面使用:

The dash needs to be the first/last character in the character class in order to be used literally:

^[-0-9a-zA-Z \/_?:.,\s]+$

^[0-9a-zA-Z \/_?:.,\s-]+$

如果不是第一个/最后一个,您也可以转义它:

You could also escape it, if not the first/last:

^[0-9a-zA-Z\- \/_?:.,\s]+$

这篇关于在正则表达式中允许-(破折号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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