Javascript Regex.test (): Returns true for double space (or more) in a string?

My goal is to return “true” for a valid string, which in this case is a string starting with a letter, then has any combination of letters and numbers and spaces, but not consecutive spaces.

I tried several combinations with the following prefix:

^[a-zA-Z][a-zA-Z0-9]*$

This works great for “starting with a letter” and “letter and number combinations,” but it's hard for me to add a regular expression to match a space and only one space.

For instance:

^[a-zA-Z][a-zA-Z0-9]*$|[\s{0,1}]

does not work. This will return true for "asdasdasd $ 3333", by the way.

This is if I try to force the logic:

if (firstLetter != letter){
    return false;
}
else{
    var spaceFound = false;
    for ( var int = 0; int < individualLetters.length; int++) {
            if (individualLetters[int] == space){
                  if (spaceFound == true){
                         return false; 
                  }
                  else {
                          spaceFound = true;     
                  }  
            }
        if(individualLetters[int] != letterOrNumber){
                      return false;
            }
            else{
                spaceFound = false;
                continue; 
            }  
    }
}

However, I think I lack a fundamental understanding of regular expression. In any case, any help would be appreciated.

+3
5

- :

function test(s) {
    return /^[A-Z]([A-Z]|\d| (?! ))*$/i.test(s);
}

http://jsfiddle.net/HCNDD/2

+4

; , , , , . :

query.match(/^[A-Za-z][A-Za-z0-9 ]*$/) && !query.match(/  /)

(...). . .

, , :

function test(query) {
    /*
     *  "My goal is to return 'true' for a valid string, which in this 
     *   case is a string that starts with a letter, then has any combination 
     *   of letters and numbers and spaces, but no consecutive spaces."
     */
    return (query.match(/^[A-Za-z][A-Za-z0-9 ]*$/) && !query.match(/  /)) == true;
}

[
    ['Z', true],
    [' ', false],
    ['_', false],
    ['9', false],
    ['A ', true],
    ['A  ', false],
    ['a\t', false],
    ['a*', false],
    ['a b C02 4', true],
    ['a b C02 4 ', true],
    ['a b C02 4  ', false],
    ['a b C02  4 ', false],
    ['a  b C02 4 ', false],
    ['a _', false],
    ['a  ', false]
].map(function(pair){
    if ( test(pair[0]) != pair[1] )
        console.log('FAILED TEST:', pair);
});

, "" , ascii, , , unicode . unicode, . Javascript + Unicode regexes


Edit

, , (?!BADPATTERN), PATTERN without SUBPATTERN within (?!.*SUBPATTERN)PATTERN. .

, , :

function test(query) {
    /*
     *  "My goal is to return 'true' for a valid string, which in this 
     *   case is a string that starts with a letter, then has any combination 
     *   of letters and numbers and spaces, but no consecutive spaces."
     */
    return Boolean( query.match(/^[A-Za-z](?!.*  )[A-Za-z0-9 ]*$/) );
    //                      START|[alpha  [alnums/spcs without |END
    //                           |       ] any ^^double spcs  ]|
}

, , . , ( ), , lookahead ( , [charset]*, ). , .

+1

:

/^[a-z]( ?[a-z0-9])*$/i

[a-z], [a-z0-9]...

0

, . , , . :

^[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$

, - , , - . , , , , . , - , , .

0

, 2 .

I didn't get the motivation for your second regex, but the reason it doesn't work is because you allow a string with one space no matter where it happens; ^ matches the beginning of a line, and $ matches the end, so you are not really writing a prefix, but an entire line; That is, if

^[a-zA-Z][a-zA-Z0-9]*$|[\s{0,1}]

mean anything it means

  • the whole line is a letter followed by any number of letters or numbers

OR

  • the line has a space somewhere in it

Maybe what you want

/^[a-zA-Z][a-zA-Z0-9 ]*$/.test(s) && !/  /.test(s) 
-1
source

All Articles