开发者

regex validating single occurences of characters

开发者 https://www.devze.com 2023-03-13 10:32 出处:网络
I want to check an input string to validate a proper text. The validation will be done with javascript and right now I\'m using this code:

I want to check an input string to validate a proper text. The validation will be done with javascript and right now I'm using this code:

keychar = String.fromCharCode(keynum);
var text = txtBox.value + keychar;
textcheck = /(?!.*(.)\1{1})^[fenFN,]*$/;
return textcheck.test(text);

The strings that are allowed are for example:

f

e

f,e

n,f,e,F,N

Examples of not allowed:

ff

fe

f,f

f开发者_高级运维,ee

f,e,n,f

n,,(although this could be ok)

Is this possible to solve with regex in Javascript?


Although it is possible using regex, it produces a rather big regex that might be hard to comprehend (and therefor maintain). I'd go for a "manual" option as Benjam suggested.

Using regex however, you could do it like this:

var tests = [
  'f',
  'e',
  'f,e',
  'n,f,e,F,N',
  'ff',
  'fe',
  'f,f',
  'f,ee',
  'f,e,n,f',
  'n,,',
  'f,e,e'
];

for(var i = 0; i < tests.length; i++) {
  var t = tests[i];
  print(t + ' -> ' + (t.match(/^([a-zA-Z])(?!.*\1)(,([a-zA-Z])(?!.*\3))*$/) ? 'pass' : 'fail'));
}

which will print:

f -> pass
e -> pass
f,e -> pass
n,f,e,F,N -> pass
ff -> fail
fe -> fail
f,f -> fail
f,ee -> fail
f,e,n,f -> fail
n,, -> fail
f,e,e -> fail

as you can see on Ideone.

A small explanation:

^                     # match the start of the input
([a-zA-Z])            # match a single ascii letter and store it in group 1
(?!.*\1)              # make sure there's no character ahead of it that matches what is inside group 1
(                     # open group 2
  ,([a-zA-Z])(?!.*\3) #   match a comma followed by a single ascii letter (in group 3) that is not repeated
)*                    # close group 2 and repeat it zero or more times
$                     # match the endof the input


I don't think you can do it with regexps alone, as they are not very good at looking around in the text for duplicates. I'm sure it can be done, but it won't be pretty at all.

What you might want to do is parse the string character by character and store the current character in an array, and while you're parsing the string, check to see if that character has already been used, as follows:

function test_text(string) {
    // split the string into individual pieces
    var arr = string.split(',');
    var used = [];

    // look through the string for duplicates
    var idx;
    for (idx in arr) {
        // check for duplicate letters
        if (used.indexOf(arr[idx])) {
            return false;
        }

        // check for letters that did not have a comma between
        if (1 < arr[idx].length) {
            return false;
        }

        used.push(arr[idx]);
    }

    return true;
}

You might also want to make sure that the browser you are running this on supports Array.indexOf by including this script somewhere: Mozilla indexOf

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号