admin管理员组

文章数量:1022804

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:

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./3ack3

Share Improve this question edited Feb 9, 2015 at 15:36 Sparky 98.8k26 gold badges202 silver badges290 bronze badges asked Feb 9, 2015 at 6:47 AdorablepolakAdorablepolak 1574 silver badges16 bronze badges 4
  • Are the rules independent or do both have to be satisfied for passed? – merlin2011 Commented Feb 9, 2015 at 6:49
  • Please post your regex here. I understand you are developing on regexr., but it will also help here if you say what language / regex style you are targeting specifically (presuming you want to use it outside of regexr.). – 1203_dube Commented Feb 9, 2015 at 6:51
  • They are independent. This regex is for a password validation, so the password may contain (if desired) the special characters above. However, repetition of the same character up to 3 times is not allowed. Hope this is now clearer. Thanks. – Adorablepolak Commented Feb 9, 2015 at 6:54
  • @1203_dube. I edited my original post with your suggestion. This is going to be used with jquery validate as an additional method. – Adorablepolak Commented Feb 9, 2015 at 6:56
Add a ment  | 

3 Answers 3

Reset to default 2

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 plex 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/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/t8609xv2/2/

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

Try this.See demo.

https://regex101./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.

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

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

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:

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./3ack3

Share Improve this question edited Feb 9, 2015 at 15:36 Sparky 98.8k26 gold badges202 silver badges290 bronze badges asked Feb 9, 2015 at 6:47 AdorablepolakAdorablepolak 1574 silver badges16 bronze badges 4
  • Are the rules independent or do both have to be satisfied for passed? – merlin2011 Commented Feb 9, 2015 at 6:49
  • Please post your regex here. I understand you are developing on regexr., but it will also help here if you say what language / regex style you are targeting specifically (presuming you want to use it outside of regexr.). – 1203_dube Commented Feb 9, 2015 at 6:51
  • They are independent. This regex is for a password validation, so the password may contain (if desired) the special characters above. However, repetition of the same character up to 3 times is not allowed. Hope this is now clearer. Thanks. – Adorablepolak Commented Feb 9, 2015 at 6:54
  • @1203_dube. I edited my original post with your suggestion. This is going to be used with jquery validate as an additional method. – Adorablepolak Commented Feb 9, 2015 at 6:56
Add a ment  | 

3 Answers 3

Reset to default 2

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 plex 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/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/t8609xv2/2/

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

Try this.See demo.

https://regex101./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.

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

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

本文标签: javascriptRegex to allow certain special charactersescape issueStack Overflow