Regex for not containing consecutive characters

regex 3 consecutive characters
regex no repeating special characters
regex not repeating characters
regex duplicate characters
regex no more than 3 characters
regex remove duplicate characters
regex not followed by
regex negative

I can't figure out javascript regex that would satisfy all those requirements:

The string can only contain underscores and alphanumeric characters. It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores.

This is as far as I came, but 'not containing consecutive underscores' part is the hardest to add.


You could use multiple lookaheads (neg. ones in this case):


See a demo on

Broken down this says:

^           # start of the line
(?!.*__)    # neg. lookahead, no two consecutive underscores (edit 5/31/20: removed extra Kleene star)
(?!.*_$)    # not an underscore right at the end
[A-Za-z]\w* # letter, followed by 0+ alphanumeric characters
$           # the end

As JavaScript snippet:

let strings = ['somestring', '_not_this_one', 'thisone_', 'neither this one', 'but_this_one', 'this__one_not', 'this_one__yes']

var re = /^(?!.*__)(?!.*_$)[A-Za-z]\w*$/;
strings.forEach(function(string) {

RegEx No more than 2 identical consecutive characters and a-Z and , I've tried adding variations of the below with no luck: /(.)\1{9,}/ .*([0-9A-Za-z])\\1+.* ((  To represent this, we use a similar expression that excludes specific characters using the square brackets and the ^ (hat). For example, the pattern [^abc] will match any single character except for the letters a, b, or c. With the strings below, try writing a pattern that matches only the live animals (hog, dog, but not bog).

You can also use



The only change comparing to your regex is changing [a-zA-Z0-9_] to [a-zA-Z0-9]|(_(?!_)). I removed underscore from the character set and allow it in the second part of the alternative if it's not followed by another one.

(?!_) is negative lookahead meaning that _ cannot be next character

What is the regex to match a string not containing more than x , Yes, it would be much easier and neater to do a regex match for containing the consecutive characters, and then negate that in the code  Regular Expression to Finds duplicated consecutive characters. Match string not containing string Validate an ip address Regex Tester isn't optimized for

Regex for strings with no three identical consecutive characters , It isn't easy to product a complement of regular expression. Very long and exhausting calculations :( For alphabet {a,b} (and if I didn't miscalculate) it is  I wanna ask what the regular expression for the strings having the property in the title should be. For binary string with no three consecutive 0, it's quite a simple regex (1|01|001)*|(0|00|$\epsi

Duplicate Consecutive, Checks the length of number and not starts with 0 · string between quotes + nested quotes · Match or Validate phone number · Match string not containing string I want to check a text for consecutive repeating characters. For example, with a minimum of 3 consecutive chars, this should match: Okeee OOOkee Alsoook This should not match: No not okee, oh no

Duplicate Consecutive, How to create a regular expression for strings of a,b and c such that aa and bb '​baz') x is a sequence of zero or more unknown characters not containing A, B,  Regular Expression to Given a list of strings (words or other characters), only return the strings that do not match.

How to create a regular expression to match non-consecutive , Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a  Your question uses a variable. Unfortunately, regular expressions are not functions so you can’t pass it a value of n. You need to know that value before you start.

  • Please tell me this is not a password format requirement.
  • Perhaps, you might write it as /^[A-Z](?!.*__)(?:\w*[A-Z0-9])?$/i
  • If this is targetting passwords, please refer to Reference - Password Validation
  • Why the \n ??
  • Looks awesome. One error tho: 'this_one__yes' <- this shouldn't match because it contains 2 consecutive underscores
  • Does not work with a or b or ab, see (meaning the string must be at least 3 characters long, dunno if this is a requirement or not).