正则表达式需要10位数字而不考虑空格 [英] Regular expression to require 10 digits without considering spaces
本文介绍了正则表达式需要10位数字而不考虑空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即使有空格,我也需要使用正则表达式将数字位数限制为10。
I need a regex to limit number digits to 10 even if there are spaces.
例如,它允许 06 15 20 47 23
与 0615204723
相同。
我尝试过: ^ \ \d {10} $
,但是如何忽略空格?
I tried: ^\d{10}$
, but how do I ignore spaces?
此外,数字应以 06开头
或 07
。 (编者注,最后一个要求来自OP的评论。)
Also, the number should start with 06
or 07
. (Editor's note, the last requirement was from comments by the OP.)
推荐答案
这将在大多数正则表达式系统中实现。 (您需要说明使用的语言!)
This will do it in most regex systems. (You need to state what language you are using!)
/^\s*(0\s*[67]\s*(?:\d\s*){8})$/
但是假设您真的只想要数字,然后继续将其分为2个步骤。
But assuming you really only want the number, then go ahead and break it into 2 steps.
例如,在JavaScript中:
For example, in JavaScript:
var s = ' 06 15 20 47 34 ';
s = s.replace (/\s/g, "");
isValid = /^0[67]\d{8}$/.test (s);
这篇关于正则表达式需要10位数字而不考虑空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文