Regular Expressions 101

Community Patterns

find a 6 lenght word with 2 consecutive digits

0

Regular Expression
ECMAScript (JavaScript)

/
(?=\w{6,})(?=\w*\d{2})
/
gmi

Description

ragex solution for free code camp positive-and-negative-lookahead

first assertion is (?=\w{6,}) where im matching for any char (a-z0-9_) and repeating the char matching 6 times, in other words, verifying that this word has 6 char being them a-z or digits.

the second assertion was more difficult to me, at first I was trying to do something like (?=\d{2}) and it was failing because when it tried to match, it was ignoring the previous chars that the word may have.

So in the second assertion, I did (?=\w*\d{2}) at first validating the word may have chars at the beginning but it has to have two consecutive digits.

When I got the two pieces together, in the first i match words that are 6 digits or more long and words that has two consecutive digits.

Submitted by gabriel silva lima - 9 months ago