用于regex检查以字符串模式开头的not contains的Javascript

时间:2022-11-25 21:45:23

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

    输入:+551234467模式:不以+55开始

  2. Input: 557875756 Pattern: Not start with 55

    输入:557875756模式:不以55开始

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

到目前为止,我已经尝试了许多模式,有些模式只对没有+的字符串给出结果。

/[^(\+55)]/

/[^(\ + 55)]/

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

/ ^(? ! \ + 55)。* /

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

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

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

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

Someone please help me on this. Thank you.

有人能帮我一下吗?谢谢你!

1 个解决方案

#1


3  

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

我忘了再提一个用例557875756也不应该匹配。意思是+55和55 - by - OP对问题的评论

Use following regex.

使用正则表达式。

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

Regex Demo and Explanation

Regex演示和解释

  1. ^: Starts with Anchor
  2. ^:始于锚
  3. \+?: Matches zero or one + symbol
  4. \ + ?:匹配0或1 +符号
  5. (?!55): Negative look-ahead, does not match 55
  6. (?!55):负面展望,不匹配55
  7. \d+: Matches one or more digits
  8. \d+:匹配一个或多个数字
  9. $: Ends with Anchor
  10. 美元:以锚

Demo

演示

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

#1


3  

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

我忘了再提一个用例557875756也不应该匹配。意思是+55和55 - by - OP对问题的评论

Use following regex.

使用正则表达式。

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

Regex Demo and Explanation

Regex演示和解释

  1. ^: Starts with Anchor
  2. ^:始于锚
  3. \+?: Matches zero or one + symbol
  4. \ + ?:匹配0或1 +符号
  5. (?!55): Negative look-ahead, does not match 55
  6. (?!55):负面展望,不匹配55
  7. \d+: Matches one or more digits
  8. \d+:匹配一个或多个数字
  9. $: Ends with Anchor
  10. 美元:以锚

Demo

演示

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