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):
test if at least one of the following special characters is entered:
!, ", #, $, %, &, ', (, ), *, +,-, .,/, :, ;, <, =, >, ?, @, [, \, ], ^, _, `, {, |, }, ~
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):
test if at least one of the following special characters is entered:
!, ", #, $, %, &, ', (, ), *, +,-, .,/, :, ;, <, =, >, ?, @, [, \, ], ^, _, `, {, |, }, ~
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
3 Answers
Reset to default 2This 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):
test if at least one of the following special characters is entered:
!, ", #, $, %, &, ', (, ), *, +,-, .,/, :, ;, <, =, >, ?, @, [, \, ], ^, _, `, {, |, }, ~
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):
test if at least one of the following special characters is entered:
!, ", #, $, %, &, ', (, ), *, +,-, .,/, :, ;, <, =, >, ?, @, [, \, ], ^, _, `, {, |, }, ~
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
3 Answers
Reset to default 2This 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
版权声明:本文标题:javascript - Regex to allow certain special characters - escape issue - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745524455a2154461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论