Regex rejects consecutive characters

I'm still very new to Regex, and basically I need to create a rule that accepts numbers and letters, but no consecutive characters can be entered.

For example: abcd β†’ ok, abbcd β†’ bad

I have most of the work, but part that I can’t determine, how exactly do I forbid consecutive characters?

My code is:

/^[A-Za-z-0-9]{8,15}$/i
+5
source share
1 answer
>>> r = /^((\w)(?!\2))+$/i
>>> r.exec('abbcd')
null
>>> r.exec('abcd')
[ 'abcd',
  'd',
  'd',
  index: 0,
  input: 'abcd' ]

\2 - , (\w). , (?!\2) " ". , , , MDN.

8-15 , OP, + {8,15}:

>>> r = /^((\w)(?!\2)){8,15}$/i
>>> r.exec('abcd')
null
>>> r.exec('abcdabcd')
[ 'abcdabcd',
  'd',
  'd',
  index: 0,
  input: 'abcdabcd' ]
+4

All Articles