正则表达式需要10位数字而不考虑空格

正则表达式需要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);