Regex Pattern to Match At Least 1 Number and 1 Character in a String

Regex pattern to match at least 1 number and 1 character in a string

Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:

/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/

Regex for alphanumeric string with at least 1 number and 1 character and a fixed length of 11

You need to combine both and use {11} for exact 11 characters match.

/^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{11}$/

Where :

  1. (?=.*\d) assert that the position follows a digit at any position(where \d is equivalent to [0-9]).
  2. (?=.*[a-zA-Z]) assert that the position follows an alphabet at any position.
  3. [a-zA-Z0-9]{11} matches only when the length is 11 characters and within the character class.
  4. ^ and $ are start and end anchors which help to check whole string.

Regex, must contain a number, 1-5 chars and letters and digits

You need to add a positive lookahead (?=\D*\d) and use {1,5} limiting quantifier at the end to match 1 to 5 chars:

^(?=\D*\d)[a-zA-Z0-9]{1,5}$

See the regex demo

Details

  • ^ - start of a string
  • (?=\D*\d) - a positive lookahead requiring a digit after 0 or more non-digit symbols
  • [a-zA-Z0-9]{1,5} - 1 to 5 (due to {1,5} limiting quantifier) consecutive alphanumeric chars (ASCII letters or digits)
  • $ - end of string.

Regex to contain at least 1 special character but not specific characters

Let's call &\;<> "invalid characters", and any other non-alphanumeric characters "special characters". A "special character" could be matched with /[^a-zA-Z0-9&\\;<>]/ - that is to say, not a-z or A-Z, not 0-9, and not any of the invalid characters.

Now our regex can search for a "special character" prefixed or suffixed by any number of valid characters:

^[^&\\;<>]*[^a-zA-Z0-9&\\;<>][^&\\;<>]*$

^ -> match start of sequence (prevent arbitrary leading characters
[^&\\;<>]* -> match 0 or more non-invalid characters
[^a-zA-Z0-9&\\;<>] -> match a mandatory special character
[^&\\;<>]* -> match 0 or more non-invalid characters
$ -> match end of sequence (prevent arbitrary trailing characters)

Test it out:

input:valid { background-color: rgba(0, 255, 0, 0.3); }input:invalid { background-color: rgba(255, 0, 0, 0.3); }
<input type="text" pattern="[^&\\;<>]*[^a-zA-Z0-9&\\;<>][^&\\;<>]*" placeholder="test strings here" required/>

Regex to match a word with at least one letter and any number of digits (no lookaround)

Regexp is built for exactly this, no need for look arounds.

The Regexp

\w*[a-zA-Z]\w*

Explanation:

  • \w: Any letter or number, *: 0 or more times
  • [a-zA-Z]: Any letter, A-Z, caps A-Z or lowercase a-z
  • \w: Any letter or number, *: 0 or more times

Regexper:

Regexper

Online Demo:

regexr.com

RegEx for at least 1 number, 1 lower case and 1 upper case letter

You are using an HTML5 pattern attribute that anchors a regex by default (it actually wraps the pattern with ^(?: and )$). That means the pattern must match the entire string. This is the reason why you can't just take out .{6,20} and keep the lookaheads.

You need to use the lookaheads at the beginning, and .* (to allow 0 or more chars in the input) or .+ (to disallow empty input) at the end (as the consuming pattern part):

pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*"

This will be successfully translated into /^(?:(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*)$/ and will work as expected:

  • ^ - start of string
  • (?: - start of the non-capturing group

    • (?=.*\d) - 1 digit after any 0+ chars from the current position is required
    • (?=.*[a-z]) - 1 lowercase letter after any 0+ chars from the current position is required
    • (?=.*[A-Z]) - 1 uppercase letter after any 0+ chars from the current position is required
  • .* - any 0 or more chars
  • ) - end of the non-capturing group
  • $ - end of string.

Regex - At least 1 number, 1 letter, 1 special character and at least 3 characters

The problem is that your {3} quantifier applies to your last look-ahead, which is nonsensical : it means that at the start of the text, you must match 3 times the look-ahead, which is a given if it matches once since lookaround are 0-width matches.

You could use the following :

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

which specifies, aside from your existing look-aheads, that at least 3 characters must be matched.

If you just test the string, it would also be enough to match

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

without an end anchor : you match 3 characters, and stop matching what follows since your requisites are met.



Related Topics



Leave a reply



Submit