代码之家  ›  专栏  ›  技术社区  ›  Gayathri Mohan

适用于Mobile的正则表达式,以09或04开头

  •  1
  • Gayathri Mohan  · 技术社区  · 9 年前

    嗨,我想为手机号码创建正则表达式

    数据验证需要10个数字。 数据验证应要求前两个数字为04或09。

    /^09\d{8}$/

    这是plunker wr my regex comming as string https://plnkr.co/edit/Yjch7G7npkHmtKYDF5Yc?p=preview

    var regex = "/^0(9|4)d{8}$/" 就像这样:(

    4 回复  |  直到 9 年前
        1
  •  6
  •   ishegg    9 年前

    /^0(9|4)\d{8}$/
    

    解释:

    ^ - means start of line
    0 - leading zero is mandatory
    (9|4) - this is saying that the zero has to be followed by either a 9 or a 4
    \d{8} - next, there have to be 8 digits
    $ - means end of line
    
        2
  •  1
  •   Arthur-1    9 年前

    这个正则表达式相当简单,可以工作:

    /(09|04)\d{8}/g

    解释:

    • (09|04) 表示匹配组(09 | 04)(组本质上是要匹配的序列),其中要匹配的序列是09或(09 | 04) | 符号)04。
    • \d{8} 表示正好匹配以下8位数字
    • 最后 g 设置全局标志(允许多个匹配)。

        3
  •  0
  •   PraveenKumar    9 年前

    我希望这对你有帮助。

    var regex = /^0(9|4)\d{8}$/
    alert(regex.test('0412345678')); //true
    alert(regex.test('0912345678')); //true
    alert(regex.test('0512345678')); //false
    
    var regex = new RegExp(/^0(9|4)\d{8}$/);
    alert(regex.test('0412345678')); //true
    alert(regex.test('0912345678')); //true
    alert(regex.test('0512345678')); //false
    
        4
  •  0
  •   AliNajafZadeh Arun Aravind    4 年前

    在asp。net代码实体您可以使用以下代码:

    RegularExpression(@"^0(9|4)[0-9\d]{8}$", ErrorMessage = "Your message")]