Right now I'm working on a client-side password validator that ensures that has:
8 characters
An uppercase letter
A lowercase letter
One number
I now need to exclude two specific characters, + and &. I wasn't exactly sure where to insert this rule in my current regular expression. What would be the best approach based on what I have now?
/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/
Match anything except +, & and \n
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[^+&\n]{8,}$
Regex Demo
Since your example is lookahead based, it makes sense to now just add a negative lookahead to preclude & or +:
/^(?!.*(\+|&))(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,}$/
Demo Here
You don't need to exclude characters + and &. You just need to mention what you want to match i.e [A-Za-z0-9]
Regex: ^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9]{8,}$
Regex101 Demo
Related
I'm building on a regular expression I found that works well for my use case. The purpose is to check for what I consider valid hashtags (I know there's a ton of hashtag regex posts on SO but this question is specific).
Here's the regex I'm using
/(^|\B)#(?![0-9_]+\b)([a-zA-Z0-9_]{1,20})(\b|\r)/g
The only problem I'm having is I can't figure out how to check if the second character is a-z (the first character would be the hashtag). I only want the first character after the hashtag to be a-z or A-Z. No numbers or non-alphanumeric.
Any help much appreciated, I'm very novice when it comes to regular expressions.
As I mentioned in the comments, you can replace [a-zA-Z0-9_]{1,20} with [a-zA-Z][a-zA-Z0-9_]{0,19} so that the first character is guaranteed to be a letter and then followed by 0 to 19 word characters (alphanumeric or underscore).
However, there are other unnecessary parts in your pattern. It appears that all you need is something like this:
/(?:^|\B)#[a-zA-Z][a-zA-Z0-9_]{0,19}\b/g
Demo.
Breakdown of (?:^|\B):
(?: # Start of a non-capturing group (don't use a capturing group unless needed).
^ # Beginning of the string/line.
| # Alternation (OR).
\B # The opposite of `\b`. In other words, it makes sure that
# the `#` is not preceded by a word character.
) # End of the non-capturing group.
Note: You may also replace [a-zA-Z0-9_] with \w.
References:
Word Boundaries.
Difference between \b and \B in regex.
The below should work.
(^|\B)#(?![0-9_]+\b)([a-zA-Z][a-zA-Z0-9_]{0,19})(\b|\r)
If you only want to accept two or more letter hashtags then change {0,19} with {1,19}.
You can test it here
In your pattern you use (?![0-9_]+\b) which asserts that what is directly on the right is not a digit or an underscore and can match a lot of other characters as well besides an upper or lower case a-z.
If you want you can use this part [a-zA-Z0-9_]{1,20} but then you have to use a positive lookahead instead (?=[a-zA-Z]) to assert what is directly to the right is an upper or lower case a-z.
(?:^|\B)#(?=[a-zA-Z])[a-zA-Z0-9_]{1,20}\b
Regex demo
I tried with many patters for username in my Angular5 application. But didn't get a suitable solution for my requirement.
The Rules are
Minimum 6 characters
Only numbers are not allowed at least one character should be there
No special characters allowed except _
No space allowed
Character only is allowed
I tried with /^[a-zA-Z0-9]+([_]?[a-zA-Z0-9])*$/
/^[a-zA-Z0-9][a-zA-Z0-9_]*[a-zA-Z0-9](?<![_\s\-]{6,}.*)$/
You can try this regex:
^[a-zA-Z0-9_]{5,}[a-zA-Z]+[0-9]*$
[a-zA-Z0-9_]{5,} to match at least five alphanumerics and the underscore
[a-zA-Z]+ to have at least one letter
[0-9]* to match zero to any occurrence of the given numbers range
Hope this helps.
You can use the following regex:
^(?=[a-z_\d]*[a-z])[a-z_\d]{6,}$
in case insensitive mode as tested on regex101: demo
Explanations:
^ anchor for the beginning of the string
$ anchor for the end of the string
(?=[a-z_\d]*[a-z]) to force the presence of at least one letter
[a-z_\d]{6,} implement the at least 6 char constraint
Yes this is fine for me. Thanks./^[a-zA-Z0-9][a-zA-Z0-9_]*[a-zA-Z0-9](?<![-?\d+\.?\d*$]{6,}.*)$/
I have done something like this
but its not working
can anyone please correct following regex.
/^[a-zA-Z.\s]+$/
You can use
/^[a-zA-Z]*$/
Change the * to + if you don't want to allow empty matches.
References:
Character classes ([...]), Anchors (^ and $), Repetition (+, *)
The / are just delimiters, it denotes the start and the end of the regex. One use of this is now you can use modifiers on it.
If you want to get only alphabets, remove . from regex. This will match all the alphabets and spaces.
/^[a-zA-Z\s]+$/
I'll also recommend you to use instead of \s
/^[a-zA-Z ]+$/
so that, other space characters(tabs, etc.) will not matched.
I'm trying to write a RegEx that returns true if the string starts with / or http: and only allows alpha numeric characters, the dash and underscore. Any white space and any other special characters should fire a false response when tested.
Below works fine (except that it allows special characters, I have not figured out how to do that yet) when tested at https://www.regex101.com/#javascript. Unfortunately returns false when I implement it in my site and test it with /products/homedecor/tablecloths. What am I doing wrong and is there a better regEx to use that would accomplish my goals?
^(\\/|(?:http:))\S+[a-zA-Z0-9-_]+$
Keep unescaped hyphen at first or at last position in character class:
^(\/|(?:http:))[/.a-zA-Z0-9_-]+$
Or even simpler:
^(\/|http:)[/\w.-]+$
Since \w is same as [a-zA-Z0-9_]
To match URL you may need to match DOT and forward slash as well.
Just remove the \S+ from your regex and put the hyphen inside the character class at the first or at the last. Note that \S+ matches any non-space characters (including non-word characters).
^(\/|http:)[a-zA-Z0-9_-]+$
For text validation for chars I am using like [a-zA-z] and for numbers like [0-9] ..if I need to add special symbols I am adding slash like [a-zA-z/-/].
While including lot of symbols its getting difficult and my javascript is getting extremely big. Is there an easy way to do it ?
Regards
A.Collins
You can take a look at this cheat sheet. for instance, [0-9] can be reduced to \d.
For the general case of "a lot of characters" — no.
\w for alphanumerics and underscores
\d for digits
\s for whitespace
You can mix them, resulting in stuff like, for example, [\d.] (for matching numbers & dots).
In a character class, x-y means "all characters between x and y". If you just have one additional character, in your case / then you don't need to use the x-y format, you can just drop the character in:
[a-zA-z/]
That's not the correct way to escape characters. \ is the correct escape character to be used:
[a-zA-Z\/]
You can use character classes, [a-zA-Z0-9] can be replaced by [\w]. The only characters that needs to be escaped are \ and -. ^ should be escaped too when it's the first character in the character class.