简体   繁体   中英

Javascript for regex to check for not contains with the starting with the string pattern

I am trying to find the matching pattern in my phone no for not starts with some string.

For example:

  1. Input: +551234467 Pattern: Not start with +55

  2. Input: 557875756 Pattern: Not start with 55

So far I have tried many patterns and some gives result for only the strings without +.

/[^(\\+55)]/

/^(?!\\+55).*$/

/[?!\\+5|55]{2}/

/[^\\+55|^\\+5|^55]/

Someone please help me on this. Thank you.

I forget to mention one more use case as 557875756 also should not match. Means +55 & 55 - by OP in comment to the question

Use following regex.

^\+?(?!55)\d+$

Regex Demo and Explanation

  1. ^ : Starts with Anchor
  2. \\+? : Matches zero or one + symbol
  3. (?!55) : Negative look-ahead, does not match 55
  4. \\d+ : Matches one or more digits
  5. $ : Ends with Anchor

Demo

 input:valid { color: green; } input:invalid { color: red; } 
 <input type="text" pattern="\\+?(?!55)\\d+" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM