简体   繁体   English

JavaScript中的正则表达式无效

[英]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. 请注意,我删除了括号:它们对test方法没用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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