How to match word in string that contain exactly "3 digits and 3 letters"?
e.g. 100BLA
var regex = ?; var string = "word word 100BLA word"; desiredString = string .match(regex);
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.
The Difference Between \s and \s+ For example, expression X+ matches one or more X characters. Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.
\d
matches a digit
[a-zA-Z]
matches a letter
{3}
is the quantifier that matches exactly 3 repetitions
^
Anchor to match the start of the string
$
Anchor to match the end of the string
So if you use all this new knowledge, you will come to a regex like this:
^\d{3}[a-zA-Z]{3}$
Since the input example has changed after I wrote my answer, here the update:
If your word is part of a larger string, you don't need the anchors ^
and $
instead you have to use word boundaries \b
.
\b\d{3}[a-zA-Z]{3}\b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With