How to Regex a string

前端 未结 2 398
南方客
南方客 2021-01-23 09:49

Is it possible to check with regex:

  1. the complete string are numbers AND
  2. the first character is a 7 or 8 then the complete length of the st
相关标签:
2条回答
  • 2021-01-23 10:32

    You can use

    ^(?:[78]\d{10}|1\d{9}|0\d{6}(?:[87]\d|\d1)\d{9})$
    

    See the regex demo

    Details

    • ^ - start of string
    • (?:[78]\d{10}|1\d{9}|0\d{6}(?:[87]\d|\d1)\d{9}) - one of the three alternatives:
      • [78]\d{10} - 7 or 8 and then 10 digits (11 all in all)
      • | - or
      • 1\d{9} - 1 and then 9 digits (10 all in all)
      • | - or
      • 0\d{6}(?:[87]\d|\d1)\d{9} - 0, then 6 digits, then the 8th digit equal to 8 or 7 and any one digit, or any one digit and the 9th digit equal to 1, and then 9 more digits (=18 digits)
    • $ - end of string.
    0 讨论(0)
  • 2021-01-23 10:39

    Here's the regex: /^[78]\d{10}$/

    "^" indicates the start of the line "$" indicates the end of the line \d means digits {10}means exactly 10 [] is a group of valid values

    The second question is dependant on wether your Engine supports Lookahead and Lookbehind https://www.regular-expressions.info/conditional.html

    0 讨论(0)
提交回复
热议问题