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 :
(?=.*\d)
assert that the position follows a digit at any position(where\d
is equivalent to[0-9]
).(?=.*[a-zA-Z])
assert that the position follows an alphabet at any position.[a-zA-Z0-9]{11}
matches only when the length is 11 characters and within the character class.^
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, capsA-Z
or lowercasea-z
\w
: Any letter or number,*
: 0 or more times
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
How to Download Fetch Response in React as File
Regex to Find Json Values Without Quotes
How to Remove All Null and Empty String Values from an Object
How to Get Date Value from One Date Input Field and Put It to Other Date Field Using JavaScript
Split a String Only the At the First N Occurrences of a Delimiter
Is It Any Limit for Post Data Size in Ajax
Typeerror: Data.Foreach Is Not a Function
Open Link in a Window Using Script (Google Sheet)
Detect If User Is Using Webview for Android/Ios or a Regular Browser
How to Remove Forward and Backward Slashes from String in JavaScript
How to Manipulate a Select Options Based on Another Select Option in HTML
How to Change Colour of Text Based on Its Value When a Page Initially Loads
Prevent React-Router History.Push from Reloading Current Route
How to Submit an HTML Form Without Redirection
How to Set a Header Field on Post a Form