Remove space from regular expression - regex

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

Related

Regex - How to include special characters in this 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")

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.).

Regex alphanumeric with hyphen, single quotes, and single spacing is timing out (crashing)

I have the following regular expression that I use but it crashes in my browsers (does nothing and then likely times out).
I am trying to accept alphanumeric, as well as dashes and single quotes. I'm also trying to restrict spacing to allow only single spaces (no more than one space consecutively)
<constant>
<constant-name>expressionFormat</constant-name>
<constant-value>^([a-zA-Z0-9'-]+\s?)*$</constant-value>
</constant>
A sample example string that crashes with this is:
"ABCDEFGHIJKLMNOPQ43 5343443RSTUVWXYZ0123456789 ‘ –"
I'm using Struts. Any tips on what I'm doing wrong? Thanks in advance!
I've found a solution.
My OLD expression:
^([a-zA-Z0-9'-]+\s?)*$
First off, I got rid of the \s since it includes other things like tabs, new lines, etc, which I do not want.
The ? is "greedy", which means if the regex fails it continues evaluating the rest of the string until it's sure it's going to return a failure... In essence, the + and ? were making it try and check recursively making it resource intensive for longer strings.
The following expression works much better for my case:
^([a-zA-Z0-9' -])*$
I believe that the browser is just taking a really long time to process the regex search and may even be timing out.
Your sample string
ABCDEFGHIJKLMNOPQ43 5343443RSTUVWXYZ0123456789 ‘ –
will not be matched by your regular expression:
^([a-zA-Z0-9'-]+\s?)*$
Add the special characters (‘ ’ — –), i.e.,
‘ ’ — –
if you want to accept them.
^([a-zA-Z0-9'‘’—–-]+\s?)*$
This regex matches your sample string.
UPDATE:
Try this regex that uses atomic grouping to avoid catastrophic backtracking:
^(?>[a-zA-Z0-9'-]+\s?)*$

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,})$