Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find 3 or more consecutive characters?

I'm making a password checking. One of those functions is to find if the inputted password is consecutively repeated. I do not have codes yet because I don't know how to do it.

I found this one RegEx match two or more same character non-consecutive but it only match repeated commas.

Here's are the scenarios:

5236aaa121 - Repeated pattern because a is consecutively repeated 3 times

2312aa32aa - No repeated character

111111asd - Repeated pattern because 1 is consecutively repeated many times

like image 593
catherine Avatar asked Mar 28 '13 17:03

catherine


People also ask

How to check for 3 consecutive identical characters in a string?

Get the String. Create a regular expression to check 3 or more consecutive identical characters or numbers as mentioned below: \\b represents the word boundary. ( represents the starting of the group 1. [a-zA-Z0-9] represents a letter or a digit.

How many consecutive characters can you write in a text file?

3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc. 3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.

How many consecutive characters/numbers can you match with a regex?

3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc. Show activity on this post. For case #2 I got inspired by a sample on regextester and created the following regex to match n identical digits (to check for both numbers and letters replace 0-9 with A-Za-z0-9 ):

Is it possible to have 3 consecutive numbers in a regular expression?

Not possible with regular expressions. 3 or more consecutive identical characters/numbers ex - 111, aaa, bbb. 222 etc. Use a pattern of (?i) (?: ( [a-z0-9])\1 {2,})*.


2 Answers

Use a back reference: /(.)\1\1/

Example:

var hasTripple = /(.)\1\1/.test('xyzzzy');

JSFiddle Example

like image 184
jmar777 Avatar answered Oct 02 '22 22:10

jmar777


Try this regex: (.)\1\1+

/(.)\1\1+/g

The dot matches any character, then we are looking for more than one in a row. i tested it on http://regexpal.com/ and i believe it does what you want

you can use this like this:

str.match(/(.)\1\1+/g).length

just check that it is 0

to see this in action.... http://jsfiddle.net/yentc/2/

like image 24
user1717674 Avatar answered Oct 02 '22 22:10

user1717674