最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

regex - javascript pattern matching - Stack Overflow

matteradmin6PV0评论

matchArray bees null for input like asklas@(((# How do I correct this behavior? I only want to allow characters and numbers..

function validateName(name) {
    debug(name);
    var namePat = /^(\[A-Za-z0-9]*)$/ ;
    var matchArray = name.match(namePat);
    if (!matchArray){
        debug ("Invalid name,", name );
        return false;
    } 
    return true;
}

matchArray bees null for input like asklas@(((# How do I correct this behavior? I only want to allow characters and numbers..

function validateName(name) {
    debug(name);
    var namePat = /^(\[A-Za-z0-9]*)$/ ;
    var matchArray = name.match(namePat);
    if (!matchArray){
        debug ("Invalid name,", name );
        return false;
    } 
    return true;
}
Share Improve this question asked May 11, 2011 at 20:44 KiranKiran 5,52613 gold badges61 silver badges85 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

There is one erroneous backslash in your regex. It should be

var namePat = /^[A-Za-z0-9]*$/;

(and you don't need the capturing parentheses, either).

Not sure what you want in this case... if you want a boolean output, use .test:

namePat.test(name)

... but null will work for your test (!matchArray) just fine.

It does seem like you have a typo in your regular expression - you'll want to get rid of the backslash before the opening bracket...

Post a comment

comment list (0)

  1. No comments so far