javascript - Regex not matching 6 repeated numbers - Stack Overflow

I am trying to get a regular expression to work but am stumped. What I want is to do the inverse of thi

I am trying to get a regular expression to work but am stumped. What I want is to do the inverse of this:

/(\w)\1{5,}/

This regex does the exact opposite of what I'm trying to do. I would like to get everything but a string that has 6 repeating numbers i.e. 111111 or 999999.

Is there a way to use a negative look-around or something with this regex?

I am trying to get a regular expression to work but am stumped. What I want is to do the inverse of this:

/(\w)\1{5,}/

This regex does the exact opposite of what I'm trying to do. I would like to get everything but a string that has 6 repeating numbers i.e. 111111 or 999999.

Is there a way to use a negative look-around or something with this regex?

Share Improve this question edited Jun 10, 2015 at 22:56 Wiktor Stribiżew 628k41 gold badges498 silver badges616 bronze badges asked Jun 10, 2015 at 20:36 HaggardWolfHaggardWolf 235 bronze badges 3
  • Is a string with 7 repeating numbers OK? Also your regex doesn't make sense. Why is 1 escaped? Are you just that isn't a regex used in a substitution regex? – Martin Konecny Commented Jun 10, 2015 at 20:42
  • What is the puting language or tool you are using? – Wiktor Stribiżew Commented Jun 10, 2015 at 21:39
  • To be clear, I am using a jQuery plugin called validation engine. I am trying to add a custom regex to the language file. I am just starting to dig into regex and am very novice. The regex I pasted above was something I found online that seems to work on regexr. to find repeating numbers. And yeah, the user can only input 6 numbers so 7 repeating numbers is ok. – HaggardWolf Commented Jun 10, 2015 at 22:25
Add a ment  | 

2 Answers 2

Reset to default 6

You can use this rgex:

/^(?!.*?(\w)\1{5}).*$/gm

RegEx Demo

(?!.*?(\w)\1{5}) is a negative lookaahead that will fail the match if there are 6 consecutive same word characters in it.

I'd rather go with the \d shorthand class for digits since \w also allows letters and an underscore.

^(?!.*(\d)\1{5}).*$

Regex explanation:

  • ^ - Start of string/line anchor
  • (?!.*(\d)\1{5}) - The negative lookahead checking if after an optional number of characters (.*) we have a digit ((\d)) that is immediately followed with 5 identical digits (\1{5}).
  • .* - Match 0 or more characters up to the
  • $ - End of string/line.

See demo. This regex will allow

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745234852a4617842.html

相关推荐

  • javascript - Regex not matching 6 repeated numbers - Stack Overflow

    I am trying to get a regular expression to work but am stumped. What I want is to do the inverse of thi

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信