Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regex matching 3 digits and 3 letters

Tags:

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); 
like image 807
InTry Avatar asked Apr 29 '13 09:04

InTry


People also ask

What is difference [] and () in 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.

What does regex 0 * 1 * 0 * 1 * Mean?

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.

What does %s mean in regex?

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.


1 Answers

\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}$ 

Update:

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 
like image 175
stema Avatar answered Sep 29 '22 21:09

stema