简体   繁体   中英

How to create optional field in regex validation (javascript)?

我正在使用https://www.debuggex.com/调试我的正则表达式,它看起来像^(\\+?\\d{2})\\d{8}$ -应该检查电话号码是否为+1212345678 (#1),如果它是+12 12345678 (#2)或只是12345678 (#3),也应该没问题,但是唯一有效的表达式是#1-我应该如何调整以使其也接受这2种模式?

Here is a fixed regex:

^(\+?\d{2}\s*)?\d{8}$
          ^^^ ^

See demo

I made the first capturing group optional with ? , and added optional whitespace \\s* .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM