简体   繁体   中英

Regular expression in JavaScript not working

Regular expression not able to read complete string, only working correct with single character.

var abc = "ab";
var patter = /^([a-z0-9A-Z])$/;

 if (patter.test(abc)) {
    console.log('yes');
 } else {
   console.log('no');
} 

You must set a quantifier when you don't want just one character.

Add a * to match zero or more character (or a + if you want to be sure there's at least one character);

var patter = /^[a-z0-9A-Z]*$/;

Note that I removed the parentheses : they're useless with the test method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM