How to make a regular expression with only three characters and three digits?

I tried the following regular expression to match here http://rubular.com/ , but it only matches 3 characters or 3 digits at a time.

^((\d{3})|(\w{3}))$ 

I need the result:

123eee

4r43fs

+7
javascript regex
source share
6 answers

If you want to use regex, this is pretty tricky:

 ^(?=.*\d.*\d.*\d)(?=.*[a-zA-Z].*[a-zA-Z].*[a-zA-Z]).{6}$ 

Look at regexr

This will do what you want.

  • \w is not what you want, it also includes \d and the underscore "_".

  • (?=.*\d.*\d.*\d) is a positive validation statement to verify the condition of three digits per line.

  • (?=.*[a-zA-Z].*[a-zA-Z].*[a-zA-Z]) is a positive validation statement to verify the condition of three letters in a string.

  • .{6} checks the length of 6 common

+7
source share

Here you go:

 ^(?=(?:[az]*\d){3})(?=(?:\d*[az]){3})\w{6}$ 

http://regex101.com/r/hO5jY9

If there are at least three digits, at least three letters and no more than six characters, the string must match.

How it works?

  • This is a classic regex for password validation.
  • Two glances verify that we have at least three numbers and at least three letters.
  • After these statements, we can match any 6 characters with \w{6} to the end of the line

Views

Let me break the first glance: (?=(?:[az]*\d){3})

He claims that three times ( {3} ) in this position in the line, which is the beginning of the line, as indicated by ^ , we can match any number of letters followed by one digit. This means that there must be at least three digits.

+8
source share

One glance should be enough to check if there are exactly 3 digits:

 ^(?=\D*(?:\d\D*){3}$)[^\W_]{6}$ 

Used by [^\W_] as an abbreviation for [A-Za-z0-9] .

test on regex101

+8
source share
 function checker(data) { var splitted = data.split(/\d/); if (splitted.length === 4) { return splitted.join("").split(/[a-zA-Z]/).length === 4; } return false; } console.assert(checker("123eee") === true); console.assert(checker("4r43fs") === true); console.assert(checker("abcd12") === false); console.assert(checker("4444ab") === false); console.assert(checker("ab1c") === false); console.assert(checker("444_ab") === false); 
+2
source share

If you just want to match any six-character combination of numbers and "text characters", use:

 /^[\d\w]{6}$/ 
0
source share

Hi, we can do this in two ways.

If you need IE7 support, the expression "?" will give some problems. So you can check as below.

 [\d\w]{6} && ![\d]{6} && ![\w]{6} 
  • check combination

  • check only the number or not

  • check only alphabets.

0
source share

All Articles