Regular Expression for validating numbers with one space and one optional special character

后端 未结 2 1929
别跟我提以往
别跟我提以往 2020-12-21 08:45

Can someone tell me how to validate sequence of numbers with one space and at the end there will be a optional special character say \'#\' and again followed by some 5 digit

2条回答
  •  甜味超标
    2020-12-21 09:09

    This should do the trick

    /^\d+\s\d+(?:#\d+)?$/
    

    See it on rubular

    ^      beginning of string
    \d+    one or more numbers
    \s     any whitespace character
    \d+    one or more numbers
    (?:    begin non-capturing group
      #    literal '#' character
      \d+  one or more numbers
    )      end non-capturing group
    $      end of string
    

    EDIT

    /^0[\d\s]{,11}(?:#\d{,5}?$/
    

    Matches a string starting with 0, followed by a max of 11 numbers or spaces. Followed by an optional # with a max of 5 numbers after it.

提交回复
热议问题