S
S
sul_evil2021-08-13 20:05:55
JavaScript
sul_evil, 2021-08-13 20:05:55

Regular expressions, why not like this?

You need to find two consecutive numbers in the text, why do you need to write exactly (?=\D*\d{2})? Not just (?=\d{2}).

let sampleWord = "banan1"; (Your regex should not match the string)
let sampleWord = "bana12"; (Your regex should match the string)
let sampleWord = "1a2bcde"; (Your regex should not match the string)

let pwRegex = /(?=\w{6})(?=\w*\d{2})/;
let result = pwRegex.test(sampleWord);

It checks if there are 6 characters in the text, and it must also contain two consecutive digits.
If we enter (?=\D*\d{2}), then it returns true, and if (?=\d{2}), false. I just want to know the mechanics, I can’t find it, as I understand it, what he does, he looks for a pattern of numbers at the beginning, but why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karpion, 2021-08-13
@Karpion

Simply is (\d{2}).
But in the example below, you have something much more complicated, it looks for more than just two digits.
In short, try to describe the action of each element of the expression, then it will become clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question