Regex:
^(?=[a-zA-Z0-9#@
)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).*
Explanation:
In the regex you provided, the lookaheads serves to ensure your string met some specific conditions, but they are not the real filter which keeps out undesired strings. You specified the following conditions:
(?=.*?[A-Z]): Match at least one uppercase letter.(?=.*?[a-z]): Match at least one lowercase letter.(?=.*?[0-9]): Match at least one number.(?=.*?[#@$?]): Match at least one of these characters:#@$?.{8,}: Match any character at least 8 times.
But there are at least two flaws:
The
(?=.*?[#@$?])part is unnecessary because those special characters are meant to be optional, not mandatory[1].As I said before, you did not specify a real filter, so thanks to the
.{8,}part, your regex will accept any string as long as it meets the conditions established by the lookaheads, even if it has undesired special characters[1].
So to solve those flaws it is necessary to:
Delete the
(?=.*?[#@$?])part.Add a new lookahead that acts as the filter mentioned above.
To construct this filter, you should think "which characters I want to allow?" instead of "which characters I want to disallow?", because that is a easier scenario to handle in this specific case. If you say you only want a-z, A-Z, 0-9 and #@$? to be your allowed characters, then the lookahead should look like this:
(?=[a-zA-Z0-9#@$?])
But hey, in this step you can even set the minimum length and tell the lookahead where to start and where to stop (the start and end of string in this case):
(?=[a-zA-Z0-9#@
)
I omitted the ^ here because it's already at the beggining of everything, so it's not necessary to be redundant. Now we just bring together all the lookaheads and match the valid password using .* instead of .{8,}:
^(?=[a-zA-Z0-9#@
)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).*
Note:
- Although, in the regex and examples you provided, I don't really know why: 1)
#@$?were not treated as mandatory; 2) undesired characters were only allowed at the end of the string and not in another place. Maybe it has something to do with the regex engine used by AWS, because everything worked as expected when I tested it on my own.
Regex:
^(?=[a-zA-Z0-9#@
)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).*
Explanation:
In the regex you provided, the lookaheads serves to ensure your string met some specific conditions, but they are not the real filter which keeps out undesired strings. You specified the following conditions:
(?=.*?[A-Z]): Match at least one uppercase letter.(?=.*?[a-z]): Match at least one lowercase letter.(?=.*?[0-9]): Match at least one number.(?=.*?[#@$?]): Match at least one of these characters:#@$?.{8,}: Match any character at least 8 times.
But there are at least two flaws:
The
(?=.*?[#@$?])part is unnecessary because those special characters are meant to be optional, not mandatory[1].As I said before, you did not specify a real filter, so thanks to the
.{8,}part, your regex will accept any string as long as it meets the conditions established by the lookaheads, even if it has undesired special characters[1].
So to solve those flaws it is necessary to:
Delete the
(?=.*?[#@$?])part.Add a new lookahead that acts as the filter mentioned above.
To construct this filter, you should think "which characters I want to allow?" instead of "which characters I want to disallow?", because that is a easier scenario to handle in this specific case. If you say you only want a-z, A-Z, 0-9 and #@$? to be your allowed characters, then the lookahead should look like this:
(?=[a-zA-Z0-9#@$?])
But hey, in this step you can even set the minimum length and tell the lookahead where to start and where to stop (the start and end of string in this case):
(?=[a-zA-Z0-9#@
)
I omitted the ^ here because it's already at the beggining of everything, so it's not necessary to be redundant. Now we just bring together all the lookaheads and match the valid password using .* instead of .{8,}:
^(?=[a-zA-Z0-9#@
)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).*
Note:
- Although, in the regex and examples you provided, I don't really know why: 1)
#@$?were not treated as mandatory; 2) undesired characters were only allowed at the end of the string and not in another place. Maybe it has something to do with the regex engine used by AWS, because everything worked as expected when I tested it on my own.
We want a lookahead anchored at the string start for each required character type as well as the length requirement. Then a simple .* to slurp it all up:
^(?=[0-9a-zA-Z#@\$\?]{8,}$)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=[^0-9]*[0-9]).*
Explanation:
First off, I decided to avoid use of the lazy quantifier when matching required char types (e.g. one uppercase) for the following reasons:
- They are expensive.
- Different regex engines have different ways of signifying lazy. (A couple don't support it at all.)
- They aren't as common/familiar as the alternative.
So for efficiency, readability and "portability" I'm using the ^[^x]*[x] construct.
Now breaking the rest down...
^ : Everything anchored to the start
(?=[0-9a-zA-Z#@\$\?]{8,}$) : Lookahead with 8 or more of your allowed characters between start and end of string.
The next three use the same pattern: a lookahead matching zero or more of a char not matching a required char, then the required char. These are all anchored to the beginning so the effect is to allow a match of the required char at any position in the string:
(?=[^a-z]*[a-z]) : At least one lowercase.
(?=[^A-Z]*[A-Z]) : At least one uppercase.
(?=[^0-9]*[0-9]) : At least one digit.
.* : Everything above is lookahead which doesn't consume anything so consume it all here. The first lookahead makes sure the entire string is valid chars so this is safe.
I make no claims about this being optimized (except for avoiding lazy quantifier). This is simply one of the easier forms to comprehend.
Note: the cause of the problem you observed with #@$? is due to the lookahead not being anchored to the end of string. Any character will match after one of those four (and not necessarily just in the last position). Of course, you can't just add $ since that then crowds out valid characters. That's why I include all valid characters in the same lookahead.
Regular Expression to restrict special characters
java - Regex to block certain special characters - Stack Overflow
java - Regular expression for excluding special characters - Stack Overflow
REGEX to escape all special characters/emojies, and #hashtags from a paragraph
You need to escape the closing bracket (as well as the backslash) inside your character class. You also don't need all the spaces:
var nospecial=/^[^*|\":<>[\]{}`\\()';@&
/;
I got rid of all your spaces; if you want to restrict the space character as well, add one space back in.
EDIT As @fab points out in a comment, it would be more efficient to reverse the sense of the regex:
var specials=/[*|\":<>[\]{}`\\()';@&$]/;
and test for the presence of a special character (rather than the absence of one):
if (specials.test(address)) { /* bad address */ }
/[$&+,:;=?[]@#|{}'<>.^*()%!-/]/
below one shouldn't allow to enter these character and it will return blank space
.replace(/[$&+,:;=?[\]@#|{}'<>.^*()%!-/]/,"");
I would just white list the characters.
^[a-zA-Z0-9äöüÄÖÜ]*$
Building a black list is equally simple with regex but you might need to add much more characters - there are a lot of Chinese symbols in unicode ... ;)
^[^<>%$]*$
The expression [^(many characters here)] just matches any character that is not listed.
To exclude certain characters ( <, >, %, and $), you can make a regular expression like this:
[<>%\$]
This regular expression will match all inputs that have a blacklisted character in them. The brackets define a character class, and the \ is necessary before the dollar sign because dollar sign has a special meaning in regular expressions.
To add more characters to the black list, just insert them between the brackets; order does not matter.
According to some Java documentation for regular expressions, you could use the expression like this:
Pattern p = Pattern.compile("[<>%\$]");
Matcher m = p.matcher(unsafeInputString);
if (m.matches())
{
// Invalid input: reject it, or remove/change the offending characters.
}
else
{
// Valid input.
}
Tried [^\W .] but it's doing the exact opposite, keeping the above-mentioned and getting rid of what I need. This is the paragraph:
To the Surfcasters of Florida , a lifestyle we all love and a passion that’s irreplaceable… we appreciate the support from all of you and as we continue to grow here is our very first performance fishing shirt now available at Floridasurfcasters.co Its land based fishing at its best! @floridasurfcasters 🎣• • • #FloridaSurfCasters 🎣 • • • #Snook #tarpon #trout #snapper #redfish #sheepshead #flounder #bluefish #mackerel #cubera #blackgrouper #permit #pompano #muttonsnapper #cobia #surffishing #jettyfishing #landbasedfishing #blacktip #blackdrum #jewfish #shark
[SOLUTION]
Couldn't figure a one and all solution so I've split it in parts of Variable Search Replace with regex:
-
Remove hashtags:
(?im)(#)\S+ -
Remove emojis:
\p{S} -
Remove unichar punctuation signs:
\p{Po} -
Remove 2 empty spaces:
\w[ ]{2,}\w -
Remove END PARAGRAPH unnecessary spacing
(?m)(?<=.)\n$
Thanks u/UnkleMike, u/aasswwddd, u/Ratchet_Guy!!!!
You just enumerated the special chars without creating a character class defined with the help of [...].
I suggest using a regex literal with a character class matching any of the symbols defined in it:
var blockSpecialRegex = /[~`!@#$%^&()_={}[\]:;,.<>+\/?-]/;
Note that the - should be at the start/end of the character class to denote a literal - symbol. The ] inside must be escaped, but [ does not have to be escaped. / must be escaped because it is a regex delimiter symbol.
JS code:
$('input').on('keypress', function (e) {
var blockSpecialRegex = /[~`!@#$%^&()_={}[\]:;,.<>+\/?-]/;
var key = String.fromCharCode(!e.charCode ? e.which : e.charCode);
console.log(key)
if(blockSpecialRegex.test(key) || $.isNumeric(key)){
e.preventDefault();
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text">
Why not use a regex to allow only alphabets,numbers and spaces(if required) ^[A-Za-z0-9 ]*$
Based on what you've given us, this may suit the bill. It will determine if any characters are not in character classes a-z, A-Z or 0-9 but note this will also treat é or similar characters as rejected symbols.
So if the value is 'test_' or 'test a' it will fail but it will pass for 'testa'. If you want it to accept spaces change the regex to /[^a-zA-Z0-9 ]/.
if(!/[^a-zA-Z0-9]/.test($(this).val())) {
$("#passwordErrorMsg").html("OK");
}
This may be helpful. javascript regexp remove all special characters if the only characters you want are numbers, letters, and ',' then you just need to whitespice all characters that are not those
$(this).val().replace(/[^\w\s\][^,]/gi, '')