Regex expression not working with once or none

左心房为你撑大大i 提交于 2019-11-27 16:31:41

The problem with the regex is that the pattern is enclosed with [ and ] that are treated as character class markers (see Character Classes or Character Sets):

With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. If you want to match an a or an e, use [ae].

In character classes, - creates ranges between literal characters. In your case, these ranges are not valid (go from a character with higher values to those with lower values) since the {4} and other subpatterns were treated as separate characters, not special constructs:

So, all you need to do is remove the [ and ] on both sides:

^4\d{3}-?\d{4}-?\d{4}-?\d{4}$

See regex demo.

Try escaping - with \ and remove [ and ]:

^4\d{3}\-?\d{4}\-?\d{4}\-?\d{4}$
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!