admin管理员组

文章数量:1332377

I'm trying to build a regex which allows the following characters:

A-Z

a-z

1234567890

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

All other characters are invalid. This is the regex I built, but it is not working as I expect it to. I expect the .test() to return false when an invalid character is present:

var string = 'abcd^wyd';

function isValidPassword () {
    var regex = /[0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]+[0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]*/g
    return regex.test(string);
}

In this case, the test is always returning "true", even when "^" is present in the string.

I'm trying to build a regex which allows the following characters:

A-Z

a-z

1234567890

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

All other characters are invalid. This is the regex I built, but it is not working as I expect it to. I expect the .test() to return false when an invalid character is present:

var string = 'abcd^wyd';

function isValidPassword () {
    var regex = /[0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]+[0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]*/g
    return regex.test(string);
}

In this case, the test is always returning "true", even when "^" is present in the string.

Share Improve this question edited Jun 17, 2016 at 18:20 Pranav C Balan 115k25 gold badges171 silver badges195 bronze badges asked Jun 17, 2016 at 17:40 developthewebzdevelopthewebz 1,9473 gold badges19 silver badges43 bronze badges 4
  • define not working as expected.. – rock321987 Commented Jun 17, 2016 at 17:41
  • @rock321987 please see updated question. – developthewebz Commented Jun 17, 2016 at 17:42
  • 2 You are not anchoring the pattern to the start/end. – PeeHaa Commented Jun 17, 2016 at 17:44
  • are there any characters that you are not allowing? – andre mcgruder Commented Jun 17, 2016 at 17:59
Add a ment  | 

5 Answers 5

Reset to default 3

Your regex only checks that at least one of the allowed characters is present. Add start and end anchors to your regex - /^...$/

var string = 'abcd^wyd';

function isValidPassword () {
    var regex = /^[0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]+[0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]*$/g
    return regex.test(string);
}

... another approach, is instead of checking all characters are good, to look for a bad character, which is more efficient as you can stop looking as soon as you find one...

// return true if string does not (`!`) match a character that is not (`^`) in the set...
return !/[^0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]/.test()

Instead of searching allowed characters search forbidden ones.

var string = 'abcd^wyd';

function regTest (string) {//[^ == not
    var regex = /[^0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]/g
    return !regex.test(string);//false if found
}

console.log(regTest(string));

The regex, as you've written is checking for the existence of the characters in the input string, regardless of where it appears.

Instead you need to anchor your regex so that it checks the entire string.

By adding ^ and $, you are instructing your regex to match only the allowed characters for the entire string, rather than any subsection.

function isValidPassword (pwd) {
    var regex = /^[0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]+[0-9A-Za-z!@#$%&*()_\-+={[}\]|\:;"'<,>.?\/\\~`]*$/g\;
    return regex.test(pwd);
}

alert(isValidPassword('abcd^wyd'));

Your regexp is matching the first part of o=your string i.e. "abcd" so it is true . You need to anchor it to the start (using ^ at the beginning) and the end of the string (using $ at the end) so your regexp should look like:

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

That way it will need to match the entire string.

You can visualize it in the following link:

regexper_diagram

This regex will work.

var str  = 'eefdooasdc23432423!@#$%&*()_-+={[}]|:;"\'<,>.?/~\`';
var reg = /.|\d|!|@|#|\$|%|&|\*|\(|\)|_|-|\+|=|{|\[|}|]|\||:|;|"|'|<|,|>|\.|\?|\/|~|`/gi;

// test it.

reg.test(str); //true

I use this site to test my regex. Regex 101

本文标签: Allowed Characters Regex (JavaScript)Stack Overflow