Regex - How to include special characters in this regex - regex

I have here a regex which accepts alphanumeric characters and also single spaces only, the problem is special characters are not included in my validation, btw I'm using react for this
Here is my code
.matches(/^[\w]+([-_\s]{1}[a-z0-9]+)*$/i, "Invalid input, please try another value")
Expected: Accept all inputs but will show error if multiple spaces is inputted without trimming the value

Regex has a symbol \S (note: not \s) which means "everything except whitespace".
If you change your regex to use \S, instead of using [a-z0-9], that should resolve your problem.
It's also worth noting that as #tripleee stated, the {1} is also entirely unnecessary, so I have removed it in my answer.
.matches(/^[\w]+([-_\s]\S+)*$/i, "Invalid input, please try another value")

Related

Regex to match one of any terms, some terms with spaces

I'm trying to write a RegEx that matches one of several terms, as part of a spam filter. The problem is, some of these terms contain spaces, and I'm having trouble writing a valid expression.
What I originally had (before multiple word temrs) was this:
(?i)(alzheimers|baldness|obese)
Now, I want to add, for example "blood pressure", but the following expression is chucking a barny:
(?i)(alzheimers|baldness|blood pressure|obese)
You can have whitespace characters in an either-or group, your expression works. Check it out for yourself:
https://regex101.com/r/56tz6B/1
Your expression should also match "blood pressure" without any problems.
Could you try to use \s+ instead of the space character and see if it works? Please note that this would also match any whitespace (tabs, new lines etc.).

Regular Expression for Password strength with one special characters except Underscore

I have the following regular expression:
^.*(?=^.{8,}$)(?=.*\d)(?=.*[!##$%^&*-])(?=.*[A-Z])(?=.*[a-z]).*$
I am using it to validate for
At least one letter
least one capital letter
least one number
least one special characters
least 8 characters
But along with this I need to restrict the underscore (_).
If I enter password Pa$sw0rd, this is validating correctly, which is true.
If I enter Pa$_sw0rd this is also validating correctly, which is wrong.
The thing is the regex is passing when all the rules are satisfied. I want a rule to restrict underscore along with above.
Any help will be very appreciable.
I think you can use a negated character class [^_]* to add this restriction (also, remove the initial .*, it is redundant, and the first look-ahead is already at the beginning of the pattern, no need to duplicate ^, and it is totally redundant since the total length limit can be checked at the end):
^(?=.*\d)(?=.*[!##$%^&*-])(?=.*[A-Z])(?=.*[a-z])[^_]{8,}$
See demo
^(?=.*?\d)(?=.*?[!##$%^&*-])(?=.*?[A-Z])(?=.*?[a-z])(?!.*_).{8,}$
You can try this..* at start is of no use.See demo.
https://regex101.com/r/pG1kU1/34

Remove space from regular expression

I do not want to show error when adding space. While using this regular expression, when adding space, shows error. How can I solve this?
return /^[A-Za-z\‘“:(),.$#!?=-_/\{}<>%^*]+$/.test(value);
I assume you mean that you want to allow spaces within value, right?
Then simply add a space to the character class. Make sure that the dash is never between two characters in the class, unless you want to specify a range (as you probably meant to do in A-Z but not in =-_). Also, this regex contains an error because you didn't escape the /:
return /^[- A-Za-z‘“:(),.$#!?=_\/{}<>%^*]+$/.test(value);
Add spaces using \s to the regex:
/^[-\sA-Za-z\‘“:(),.$#!?=_\/\{}<>%^*]+$/
As #Tim suggested below, / should be escaped and the - should not come between two characters for which it will define a range. Answer was updated respectfully.
Second, as was noted in the comments below, it's worth mentioning that \s matches all types of spaces (tabs, newlines etc) which is even more general than what the OP asked for

Regular expression to allow alpha, a few special characters or an empty string

I have my regular expression that is validating a form field and allows only alpha and a few selected special characters.
But this field is optional, so I need to make it allowing also empty strings.
My current regex is /^[a-z ,.'-]+$/i
I tried various solutions to add the empty string option, but nothing worked.
Any suggestions ?
Change the + to *, which updates the meaning from 1+ repeats to 0+ repeats (essentially making it optional).
/^[a-z ,.'-]*$/i
^
Also, I suggest using \s instead of an empty space if possible since it makes more sense to someone used to reading regular expressions and you won't miss it at first glance. Please note that \s is shorthand for [\r\n\t\f ], so it will also match newlines and tabs (i.e. it may not work for your scenario).
Okay, I just sorted it out. This one seems to do the trick. If you see any flaw on it, please let me know.
/^$|[a-z ,.'-]+$/i

Password validation regex

I am trying to get one regular expression that does the following:
makes sure there are no white-space characters
minimum length of 8
makes sure there is at least:
one non-alpha character
one upper case character
one lower case character
I found this regular expression:
((?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])(?!\s).{8,})
which takes care of points 2 and 3 above, but how do I add the first requirement to the above regex expression?
I know I can do two expressions the one above and then
\s
but I'd like to have it all in one, I tried doing something like ?!\s but I couldn't get it to work. Any ideas?
^(?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])\S{8,}$
should do. Be aware, though, that you're only validating ASCII letters. Is Ä not a letter for your requirements?
\S means "any character except whitespace", so by using this instead of the dot, and by anchoring the regex at the start and end of the string, we make sure that the string doesn't contain any whitespace.
I also removed the unnecessary parentheses around the entire expression.
Tim's answer works well, and is a good reminder that there are many ways to solve the same problem with regexes, but you were on the right track to finding a solution yourself. If you had changed (?!\s) to (?!.*\s) and added the ^ and $ anchors to the end, it would work.
^((?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,})$