简体   繁体   中英

Regex to allow certain special characters - escape issue

I've working on a javascript regex that I intend to use with the jquery validate plugin (I'll add this as an additional method). It must (among other rules):

  1. test if at least one of the following special characters is entered:

     !, ", #, $, %, &, ', (, ), *, +,-, .,/, :, ;, <, =, >, ?, @, [, \\, ], ^, _, `, {, |, }, ~ 
  2. not allow 3 consecutive identical characters:

passed:

aa
99
++

not passed:

aaa
999
+++

The problem with my regex is that is having problem with these mentioned rules: I think the issue is related to escaping and I've tried escaping + and - to no avail. Can anyone help! This is my regex: http://regexr.com/3ack3

This is one of those requirements where you can really simplify your life by using multiple regexes, rather than trying to cram all the logic into one complex regex with many assertions. Here's some JavaScript that implements your requirement:

var specialCharRegex = /[!"#$%&'()*+.\/:;<=>?@\[\\\]^_`{|}~-]/;
var threeConsecutiveRegex = /(.)\1\1/;

var input = prompt();
if (specialCharRegex.test(input) && !threeConsecutiveRegex.test(input)) {
    alert('passed');
} else {
    alert('failed');
} // end if

http://jsfiddle.net/t8609xv2/

Some notes on the trickier points:

  • inside the bracket expression, the following four special characters had to be backslash-escaped: /[\\] . (Forward slash because it delimits the regex, backslash because it's the escape character, and the brackets because they delimit the bracket expression.)
  • inside the bracket expression, the dash had to be moved to the end, because otherwise it would likely specify a character range. When at the end, it never specifies a range, so it's always safer to put it there.

This modular approach also benefits maintainability, as you will more easily be able to make changes (modify/add/remove regexes, or change the if-test logic) at a later point in time.

Another benefit is that you could test each regex independently, which could allow you to provide a more accurate error message to the user, as opposed to just saying something like "invalid password".

Edit: Here's how you can whitelist the chars that are accepted in the input:

var specialCharRegex = /[!"#$%&'()*+.\/:;<=>?@\[\\\]^_`{|}~-]/;
var threeConsecutiveRegex = /(.)\1\1/;
var nonWhitelistCharRegex = /[^a-zA-Z0-9!"#$%&'()*+.\/:;<=>?@\[\\\]^_`{|}~-]/;

var input = prompt();
if (specialCharRegex.test(input) && !threeConsecutiveRegex.test(input) && !nonWhitelistCharRegex.test(input)) {
    alert('passed');
} else {
    alert('failed');
} // end if

http://jsfiddle.net/t8609xv2/2/

You can use this regex:

^(?!.*?(.)\1{2})(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*?[!@#$%^&*()_=\[\]{};':"\\|,.<>\/?+-]).{8,20}$

RegEx Demo

You might be able to shorten it using:

^(?!.*?(.)\1{2})(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*?[\W_]).{8,20}$

ie using non-word property \\W instead of listing each and every special character.

^(?=.*[!"#$%&'()*+,,\/:;<=>?@\[\]^_`{|}~-])(?!.*(.)\1\1).*$

Try this.See demo.

https://regex101.com/r/wX9fR1/10

You need a positive lookahead to check for special characters .

And

A negative lookahead to check if a character is is there 3 times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM